2016-03-06 118 views
0

当我尝试使用set_edge_attributes在networkx为整数值分配给新属性为所有edeges(有一个无向networkx摹话):得到错误

nx.set_edge_attributes(G, 'new_attr', 1) 

错误显示

AttributeError: 'int' object has no attribute 'items'

但由于受https://networkx.github.io/documentation/latest/reference/generated/networkx.classes.function.set_edge_attributes.html

If values is not a dictionary, then it is treated as a single attribute value that is then applied to every edge in G.

所以应该OK为图中的每条边设置相同的整数值?或者我不能'创造'一个新的属性,必须使用现有的属性?

更新: 看来,我可以用

G.edge[u][v]['new_attr'] 

创建和访问的属性。但是有没有一种更简单的方法来一次赋值而不使用循环?我需要使用相同的属性初始值初始化网络。

回答

0

这是正确的想法。例如

In [1]: import networkx as nx 

In [2]: G = nx.path_graph(4) 

In [3]: nx.set_edge_attributes(G,'foo',1) 

In [4]: list(G.edges(data=True)) 
Out[4]: [(0, 1, {'foo': 1}), (1, 2, {'foo': 1}), (2, 3, {'foo': 1})] 

也许你的图G损坏?如果您需要更多帮助,请发布完整示例。

+0

谢谢!它工作正常。我不知道为什么昨天没有工作。 – Jingjin

+0

顺便说一句,我意识到,在我的其他电脑有Python 2.7.3版本这个错误信息仍然弹出。在这种情况下,我需要先更新我的Python吗? – Jingjin