Reverse dictionnary
Uwe Schmitt
uwe.schmitt at procoders.net
Fri Nov 15 07:24:32 EST 2002
Julien Barbot <barbot at shfj.cea.fr> wrote:
> Hi,
> Does "reverse dictionnary" exists in python ?
> I would like to get a value with the key, and to get the key with the value
> For example:
> d1 = {1:"str1", 2:"str2" }
> d1.getvalue(1) give me "str1"
> and
> g1.getkey("str1") give me 1
> With the result given as fast as normal dictionnaries.
What about
d= {1:'one', 2:'two' }
temp_list = [ (d[k],k) for k in d.keys() ]
d_reverse = dict(temp_list)
maybe
d_reverse=dict(zip(d.values(), d.keys())
works, but I'm not sure if d.values() and d.keys() are allways
in right order...
Greetings, Uwe
--
Dr. rer. nat. Uwe Schmitt Computer science is no more about Computers,
uwe.schmitt at num.uni-sb.de than astronomy is about telescopes. (Dijkstra)
http://www.procoders.net
More information about the Python-list
mailing list