存储和访问节点属性 python networkx

我有一个使用 pythonnetworkx创建的节点网络。我想在节点中存储信息,这样我就可以在以后根据节点标签(节点的名称)和存储信息的字段(如节点属性)访问信息。存储的信息可以是一个字符串或数字,我希望这样做的方式是,如果 xyz是一个节点:

然后我想保存两个或三个字段有字符串,比如 xyz dob=1185的出生日期,xyz pob=usa的出生地点,以及 xyz dayob=monday的出生日期。

我知道我可以使用 G.add_node的属性字典字段在它... 但我似乎不能访问它的特定领域。如果还有别的办法,我会很感激的。

然后,我想比较 xyz与网络中其他节点有相同的共同信息。即根据出生日期、出生地点和出生日期,将节点 xyz与节点 abc相交

例如,如果节点 xyzabc有一个边打印它们各自的 dob,它们的 pob和它们的 dayob

111653 次浏览

As you say, it's just a matter of adding the attributes when adding the nodes to the graph

G.add_node('abc', dob=1185, pob='usa', dayob='monday')

or as a dictionary

G.add_node('abc', {'dob': 1185, 'pob': 'usa', 'dayob': 'monday'})

To access the attributes, just access them as you would with any dictionary

G.node['abc']['dob'] # 1185
G.node['abc']['pob'] # usa
G.node['abc']['dayob'] # monday

You say you want to look at attributes for connected nodes. Here's a small example on how that could be accomplished.

for n1, n2 in G.edges_iter():
print G.node[n1]['dob'], G.node[n2]['dob']
print G.node[n1]['pob'], G.node[n2]['pob']
# Etc.

As of networkx 2.0, G.edges_iter() has been replaced with G.edges(). This also applies to nodes. We set data=True to access attributes. The code is now:

for n1, n2 in list(G.edges(data=True)):
print G.node[n1]['dob'], G.node[n2]['dob']
print G.node[n1]['pob'], G.node[n2]['pob']
# Etc.

NOTE: In networkx 2.4, G.node[] has been replaced with G.nodes[].

Additionally, you don't have to just assign the attributes when the node is added. Even after it's been added you can still set them directly.

import networkx as nx
G=nx.Graph()
G.add_edge(1,2)
#see comment below code for recent versions of networkx.
G.nodes[1]['name'] = 'alpha'
G.nodes[2]['name'] = 'omega'


G.nodes[1]['name']
> 'alpha'

Note: For versions before 2.4, use G.node[] instead of G.nodes[]. See deprecation notes.

You can also use set_node_attributes (documentation) which will let you set an attribute for multiple nodes at the same time:

edit

#use the next line if it's networkx version 1.x
#nx.set_node_attributes(G, 'cost', {1:3.5, 2:56})


#use the next line if it's networkx version 2.x
#nx.set_node_attributes(G, {1:3.5, 2:56}, 'cost')


#or for something that works for 1.x or 2.x
nx.set_node_attributes(G, values = {1:3.5, 2:56}, name='cost')


G.node[1]['cost']
> 3.5

Similar approaches can be used to set edge attributes.

As of networkx v2.0, you can use:

import networkx as nx


G = nx.Graph()
G.add_node('abc', dob=1185, pob='usa', dayob='monday')
nx.get_node_attributes(G, 'dob')
> {'abc': 1185}

You can access this dictionary as usual:

{'abc': 1185}['abc']
> 1185

Apparently now

G.node[1]['name'] = 'alpha'

do not work anymore.

I used this : https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.nodes.html

adding an s at node :

G.nodes[1]['name'] = 'alpha'

To add attributes as dictionary you can do the following

g.add_node('node_id', **{"attr1": "val1", "attr2": "val2"})

p.s. if you don't add ** you'll get exception: TypeError: add_node() takes 2 positional arguments but 3 were given