This question could sound something stange, but normally you can get values by the key, e.g:
>>> mydict = {'a':1,'b':2,'c':3} >>> mydict['a'] 1 >>> mydict['b'] 2 >>> mydict['c'] 3 But I need to do:
>>> mydict[1] 'a' >>> mydict[2] 'b' >>> mydict[3] 'c' # In this case, my dictionary have to work like >>> mydict = {1:'a',2:'b',3:'c'} In my program my dictionary could be open by the two ways, I mean:
>>> mydict = {'a':1,'b':2,'c':3} # Sometimes I need the value of a letter: >>> mydict['a'] 1 # And somethimes, I need the letter of a value. >>> mydict.REVERSAL[1] a I can do something like: (I don't know if this work, I don't test it)
>>> mydict = {'a':1,'b':2,'c':3} >>> mydict['a'] 1 # etc... >>> def reversal(z): ... tmp = {} ... for x,y in z.items(): ... tmp[y] = x ... return tmp >>> mydict = reversal(mydict) >>> mydict[1] a # etc >>> mydict = reversal(mydict) >>> mydict['c'] 3 # etc, etc, etc... Is there an easy way to do that?
FIRST: I know about chr() and ord(), my code isn't about letters... this is only and example.
SECOND: In my dictionary there won't be two same values, so there won't be any problems with duplicate keys...
dand{v:k for k,v in d.items()}