假设我有一个如下所示的数据集,显示了无向图:
1 2
1 3
1 4
3 5
3 6
7 8
8 9
10 11
我有一个像这样的python脚本:
for s in ActorGraph.degree():
print(s)
这是一个由键和值组成的字典,键是节点名,值是节点度:
('9', 1)
('5', 1)
('11', 1)
('8', 2)
('6', 1)
('4', 1)
('10', 1)
('7', 1)
('2', 1)
('3', 3)
('1', 3)
在networkx文档中,建议使用values()获得节点度。
现在我喜欢只具有节点度数的键,我使用了脚本的这一部分,但是它不起作用并说
object has no attribute 'values'
:for s in ActorGraph.degree():
print(s.values())
我该怎么做?
最佳答案
您正在使用networkx的2.0版。从使用dict
的G.degree()
变为使用类似dict(但不是dict)的DegreeView
。参见this guide。
要在列表中显示学位,可以使用list-comprehension:
degrees = [val for (node, val) in G.degree()]
关于python-3.x - 通过python中的Networkx获取图中每个节点的度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47433523/