Dictionary bidirectional
Ken Starks
straton at lampsacos.demon.co.uk
Mon Jul 14 04:44:34 EDT 2008
Dennis Lee Bieber wrote:
> On Sun, 13 Jul 2008 16:21:11 -0700 (PDT), Kless
> <jonas.esp at googlemail.com> declaimed the following in comp.lang.python:
>
>> I need a dictionary where get the result from a 'key' (on left), but
>> also from a 'value' (on right), how to get it?
>>
>> I know that dictionaries aren't bidirectional, but is there any way
>> without use two dictionaries?
>>
> Just out of curiosity... What do you expect to have returned from...
>
> aDict = { "one" : "two",
> "three" : "four",
> "What?" : "two" }
>
> when looking for the value "two"?
>
> In a dictionary, the /keys/ are unique... but the /values/ can be
> duplicates.
I wonder if anyone has implemented an 'equivalence class' class (for
finite sets) based on this.
Obviously the relation defined by
k1~k2 iff D[k1] == D[k2]
does partition the set of all keys as an equivalence class.
So ... as a kind of inverse you could return a set, a subset of the
keys. How you would get a canonical representative of that set is
a different matter, of course. Unless, as in the OP's case, it is
a singleton set.
It would seem more efficient to do this when a key-value pair is
added or removed from the original dictionary rather than iterating
over all the keys each time you used it.
More information about the Python-list
mailing list