Note that there are some explanatory texts on larger screens.

plurals
  1. POIn python, how can you retrieve a key from a dictionary?
    primarykey
    data
    text
    <p>I have a hashable identifier for putting things in a dictionary:</p> <pre><code>class identifier(): def __init__(self, d): self.my_dict = d self.my_frozenset = frozenset(d.items()) def __getitem__(self, item): return self.my_dict[item] def __hash__(self): return hash(self.my_frozenset) def __eq__(self, rhs): return self.my_frozenset == rhs.my_frozenset def __ne__(self, rhs): return not self == rhs </code></pre> <p>I have a node type that encapsulates identifer for purposes of hashing and equality:</p> <pre><code>class node: def __init__(self, id, value): # id is of type identifier self.id = id self.value = value # define other data here... def __hash__(self): return hash(self.id) def __eq__(self, rhs): if isinstance(rhs, node): return self.id == rhs.id ### for the case when rhs is an identifier; this allows dictionary ### node lookup of a key without wrapping it in a node return self.id == rhs def __ne__(self, rhs): return not self == rhs </code></pre> <p>I put some nodes into a dictionary:</p> <pre><code>d = {} n1 = node(identifier({'name':'Bob'}), value=1) n2 = node(identifier({'name':'Alex'}), value=2) n3 = node(identifier({'name':'Alex', 'nationality':'Japanese'}), value=3) d[n1] = 'Node 1' d[n2] = 'Node 2' d[n3] = 'Node 3' </code></pre> <p>Some time later, I have only an identifier:</p> <pre><code>my_id = identifier({'name':'Alex'}) </code></pre> <p>Is there any way to efficiently lookup the node that has been stored with this identifier in this dictionary?</p> <p>Please note that this is a little trickier than it sounds; I know that I can trivially use <code>d[my_id]</code> to retrieve the associated item <code>'Node 2'</code>, but <strong>I want to efficiently return a reference to <code>n2</code></strong>.</p> <p>I know that I could do it by looking at every element in <code>d</code>, but I've tried that and it's much too slow (the dictionary has thousands of items in it and I do this a fair number of times).</p> <p>I know that internally <code>dict</code> is using the <code>hash</code> and <code>eq</code> operators for that identifier to store node <code>n2</code> and its associated item, <code>'Node 2'</code>. In fact, using <code>my_id</code> to lookup <code>'Node 2'</code> actually needs to lookup <code>n2</code> as an intermediate step, so <strong>this should definitely be possible.</strong> </p> <p>I am using this to store data in a graph. The nodes have a lot of additional data (where I put <code>value</code>) that is not used in the hash. I didn't create the graph package I'm using (networkX), but I can see the dictionary that stores my nodes. I could also keep an extra dictionary around of identifiers to nodes, but this would be a pain (I'd need to wrap the graph class and rewrite all add node, remove node, add nodes from list, remove nodes from list, add edge, etc. type functions to keep that dictionary up to date).</p> <p><strong>This is quite the puzzle. Any help would be really appreciated!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload