存储和访问节点属性 python networkx

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13698352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:24:26  来源:igfitidea点击:

Storing and Accessing node attributes python networkx

pythonattributesnetworkx

提问by user1295112

I have a network of nodes created using python networkx. i want to store information in nodes such that i can access the information later based on the node label (the name of the node) and the field that in which the information has been stored (like node attributes). the information stored can be a string or a number I wish to do so in a manner such that if xyzis a node:

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

then I want to save two or three fields having strings like the date of birth of xyzdob=1185, the place of birth of xyzpob=usa, and the day of birth of xyzdayob=monday.

那么我想保存有像的出生日期字符串两个或三个字段xyzdob=1185,出生地点xyzpob=usa和出生的那一天xyzdayob=monday

I know that i can use G.add_nodehas the attribute dictionary field in it...but I can't seem to access it for a particular field. if there is any other way i would appreciate it.

我知道我可以使用G.add_node其中的属性字典字段......但我似乎无法访问它的特定字段。如果有任何其他方式,我将不胜感激。

i then want to compare xyzwith other nodes in the networks having the same information in common. i.e. intersection of node xyzwith node abcbased on date of bith, place of birth and day of birth

然后我想xyz与网络中具有相同信息的其他节点进行比较。即节点xyzabc基于出生日期、出生地点和出生日期的节点的交集

e.g for if nodes xyzand abchave an edge print their respective dobs, their pobs and their dayobs

例如,如果节点xyzabc边缘打印它们各自的dobs、它们的pobs 和它们的dayobs

采纳答案by Maehler

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=Trueto access attributes. The code is now:

从 networkx 2.0 开始,G.edges_iter() 已替换为 G.edges()。这也适用于节点。我们设置data=True访问属性。现在的代码是:

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[].

注意:networkx 2.4 中G.node[]已替换为G.nodes[].

回答by Joel

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.node[1]['name'] = 'alpha'
G.node[2]['name'] = 'omega'

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

Note:In version 2.4+, use G.nodes[]instead of G.node[]. See deprecation notes.

注意:在 2.4+ 版本中,使用G.nodes[]代替G.node[]. 请参阅弃用说明。

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

您还可以使用set_node_attributes文档),它可以让您同时为多个节点设置一个属性:

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.

类似的方法可用于设置边缘属性。

回答by severinsimmler

As of networkxv2.0, you can use:

networkxv2.0 开始,您可以使用:

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

回答by Marine Galantin

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

我用过这个:https: //networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.nodes.html

adding an s at node :

在节点添加一个 s :

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