Worthwhile to reverse a dictionary
rudysanford at hotmail.com
rudysanford at hotmail.com
Wed Dec 14 13:43:55 EST 2005
Thanks so much. That seems to have it. This is the sort of thing I
had before:
#!/usr/local/bin/python
# make a short dictionary
d1 = {'A' : '1', 'B' : '2', 'C' : '3'}
for letter in d1.keys(): print letter, '\t', d1[letter]
# make a space between the output of the 2 dictionaries
print '\n'
# reverse the keys and values from the first dictionary
# from this point many things were tried, about the best I achieved was
# getting the last key/value pair as displayed when printing the d1
# it was properly reversed but there was only one pair
d2 = {d1[letter] : letter}
for num in d2.keys(): print num, '\t', d2[num]
This displays
A 1
C 3
B 2
2 B
More information about the Python-list
mailing list