Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93052 > unrolled thread

Looking up a dictionary _key_ by key?

Started byDan Stromberg <drsalists@gmail.com>
First post2015-06-23 17:21 -0700
Last post2015-06-24 11:49 +1000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Looking up a dictionary _key_ by key? Dan Stromberg <drsalists@gmail.com> - 2015-06-23 17:21 -0700
    Re: Looking up a dictionary _key_ by key? Paul Rubin <no.email@nospam.invalid> - 2015-06-23 18:02 -0700
    Re: Looking up a dictionary _key_ by key? Steven D'Aprano <steve@pearwood.info> - 2015-06-24 11:49 +1000

#93052 — Looking up a dictionary _key_ by key?

FromDan Stromberg <drsalists@gmail.com>
Date2015-06-23 17:21 -0700
SubjectLooking up a dictionary _key_ by key?
Message-ID<mailman.0.1435105323.3674.python-list@python.org>
I know that sounds strange: usually we look up values by key, not keys.

But suppose you have a strange key type that despite being "equal", is
not identical in some fields, and you need to see those fields.

Is there a way of getting the key used by the dictionary, short of
storing a reference to it in the value, or using a second dictionary?

Thanks!

[toc] | [next] | [standalone]


#93056

FromPaul Rubin <no.email@nospam.invalid>
Date2015-06-23 18:02 -0700
Message-ID<87h9pxyl3b.fsf@nightsong.com>
In reply to#93052
Dan Stromberg <drsalists@gmail.com> writes:
> suppose you have a strange key type that despite being "equal", is
> not identical in some fields

Don't do that.  You were asking for trouble and you got some.

[toc] | [prev] | [next] | [standalone]


#93060

FromSteven D'Aprano <steve@pearwood.info>
Date2015-06-24 11:49 +1000
Message-ID<558a0ca5$0$1670$c3e8da3$5496439d@news.astraweb.com>
In reply to#93052
On Wed, 24 Jun 2015 10:21 am, Dan Stromberg wrote:

> I know that sounds strange: usually we look up values by key, not keys.
> 
> But suppose you have a strange key type that despite being "equal", is
> not identical in some fields, and you need to see those fields.
> 
> Is there a way of getting the key used by the dictionary, short of
> storing a reference to it in the value, or using a second dictionary?

A nice problem from an ugly situation. The short answer is, no.

Python's dictionary lookup algorithm could be something like this:

- take the hash of the key, h;
- inspect slot h of the internal array;
- if it contains a key equal to the input key, return the value
- otherwise deal with a collision somehow


You're basically asking for step 3 to return the input key instead of the
value. Short of re-writing the dict type, no, there is no way to do so.

Without knowing your application, I can't really know what the right
approach is. One might be to ensure that only a canonical version of the
key is used in the first place, by interning it.


ALIVE = {}
def intern(key):
    return ALIVE.setdefault(key, key)


If you pass two objects that are equal but not identical, the first one will
become the canonical interned value, and the second time you will get the
first one back.

If interning doesn't suit, you can do a linear search through the keys to
find the one which matches:

for k in mydict:
    if k == key:
        assert mydict[k] is mydict[key]
        print(k, "equals but is not identical to", key)


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web