In python, how can you get the key from a dictionary?

I have a hashed identifier to put things in a dictionary:

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

I have a node type that encapsulates an identifier for hashing and equality purposes:

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

I put some nodes in the dictionary:

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'

After a while, I only have an identifier:

my_id = identifier({'name':'Alex'})

Is there a way to efficiently look for a node that has been stored with this identifier in this dictionary?

Note that this is a little more complicated than it sounds; I know that I can use it trivially d[my_id]to get the associated item 'Node 2', but I want to effectively return the link ton2 .

, , d, , ( , ).

, dict hash eq node n2 , 'Node 2'. , my_id 'Node 2' n2 , .

. ( value), . , (networkX), , . , ( add node, node, , list, add edge .., ).

. !

+5
5

d[n1] = 'Node 1'

:

d[n1] = ('Node 1', n1)

n1 , .

, , k1, , , - k2, k1.

+5

.  - , / , , /.

:

# When adding a value:
d[n2] = value;
# Must also add to the reverse dictionary:
rev[value] = d

# This means that:
value = d[n2]
# Will be able to efficiently find out the key used with:
key = rev[value]
+3

node NetworkX. "node" , , . .

import networkx as nx

class Node(object):

    def __init__(self,id,**attr):
        self.id=id
        self.properties={}
        self.properties.update(attr)

    def __hash__(self):
        return self.id

    def __eq__(self,other):
        return self.id==other.id

    def __repr__(self):
        return str(self.id)

    def __str__(self):
        return str(self.id)


G=nx.Graph()
# add two nodes
n1=Node(1,color='red') # the node id must be hashable
n2=Node(2,color='green')
G.add_node(n1,obj=n1)
G.add_node(n2,obj=n2)

# check what we have
print G.nodes() # 1,2
print n1,n1.properties['color'] # 1,red
print n1==n2   # False 
for n in G:
    print n.properties['color']
print Node(1) in G # True
# change color of node 1
n1.properties['color']='blue'
for n in G:
    print n.properties

# use "node attribute" data in NetworkX to retrieve object
n=G.node[Node(1)]['obj']
print type(n) # <class '__main__.Node'>
print n # 1
print n.id # 1
print n.properties # {'color': 'blue'}

, , , :

   def get_node(G,n):
        return G.node[Node(1)]['obj']

    n=get_node(G,1)
    print n.properties
+1

, , Node. ,

d[my_id]=d[my_id] 

, , , Node. "", , . Node , Node , Node .

( ), , ,

0

my_id 'Node 2' n2

. - -: () . d[my_id], Python hash(my_id), d. , hash(n1) == hash(id1), .

. , .


, ? , node identifier({'name':'Alex'}), node? , :

class Node:
    def __init__(self, id, value):
        id.parent = self
        ...
0

All Articles