Reverse dictionnary
Padraig Brady
Padraig at Linux.ie
Fri Nov 15 06:33:03 EST 2002
Julien Barbot 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
The (hash) mapping is one way only.
Consider d1 = {1:"str1", 2:"str2", 3:"str2"}
What would you do in this case?
If your sure there is a one to one mapping
and you only want to store the data once
and have a slow reverse mapping you could do.
d1.keys()[d1.values().index('str1')]
Pádraig.
More information about the Python-list
mailing list