Worthwhile to reverse a dictionary
crc
crc.214rin at no-mx.forums.yourdomain.com.au
Wed Jan 4 16:30:28 EST 2006
I assume your talking about building a new dictionary with the key and
value pair switched. I have seen no built in function to do this but I
have found a way to create another dictionary that is the inverse of
the first.
for example you have designed a dictionary such as
a={}
a['one']=1
a['two']=2
a['three']=3
a['four']=4
when your redy to build a revers dictionary you semply do the folowing
b={}
for x in a:
temp_idx=a[x]
b[temp_idx]=x
now you get
{'four': 4, 'three': 3, 'two': 2, 'one': 1}
for a
and
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
for b
--
crc
Message posted via http://www.exforsys.com for all your training needs.
More information about the Python-list
mailing list