通过Networkx在python中获取图形中每个节点的度数(Get degree of each nodes in a graph by Networkx in python)
假设我有一个如下所示的数据集,显示了一个无向图:
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())
我该怎么做?
Suppose I have a data set like below that shows an undirected graph:
1 2
1 3
1 4
3 5
3 6
7 8
8 9
10 11
I have a python script like it:
for s in ActorGraph.degree():
print(s)
that is a dictionary consist of key and value that keys are node names and values are degree of nodes:
('9', 1)
('5', 1)
('11', 1)
('8', 2)
('6', 1)
('4', 1)
('10', 1)
('7', 1)
('2', 1)
('3', 3)
('1', 3)
In networkx documentation suggest to use values() for having nodes degree. now I like to have just keys that are degree of nodes and I use this part of script but it does't work and say object has no attribute 'values':
for s in ActorGraph.degree():
print(s.values())
how can I do it?
原文:https://stackoverflow.com/questions/47433523
更新时间:2020-03-14 19:03
最满意答案
您正在使用networkx的2.0版本。 从使用G.degree()的词典变为使用类似字典(但不是dict)的DegreeView 。 请参阅本指南 。
要获得列表中的学位,您可以使用列表理解 :
degrees = [val for (node, val) in G.degree()]
You are using version 2.0 of networkx. Which changed from using a dict for G.degree() to using a dict-like (but not dict) DegreeView. See this guide.
To have the degrees in a list you can use a list-comprehension:
degrees = [val for (node, val) in G.degree()]
2017-11-23
相关问答
从文档 : 节点颜色。 可以是单个颜色格式的字符串,也可以是与nodelist具有相同长度的一系列颜色。 如果指定了数值,则使用cmap和vmin,vmax参数将它们映射到颜色。 有关更多详细信息,请参阅matplotlib.scatter。 part = community.best_partition(G)为每个节点分配社区 - part是dict, part[node]是节点所属的社区(每个都分配了一个整数)。 后来的values = [part.get(node) for node in
...
以下作品: sorted(G.degree, key=lambda x: x[1], reverse=True)
The following works: sorted(G.degree, key=lambda x: x[1], reverse=True)
我不是networkx专家,但根据这里的文档: Multigraphs NetworkX为图形提供类,允许任何一对节点之间有多条边。 MultiGraph和MultiDiGraph类允许您两次添加相同的边,可能使用不同的边数据。 这对于某些应用程序来说可能很强大,但是很多算法在这些图表上没有很好的定义。 最短路径就是一个例子。 如果结果明确定义,例如MultiGraph.degree(),我们提供该函数。 否则,您应该转换为标准图形,以使测量得到明确定义。 因此,如果进行了一项更改,该示例应该可以
...
给定具有一组节点的图G ,最简单的事情就是 mapping = {old_label:new_label for new_label, old_label in enumerate(G.nodes())}
H = nx.relabel_nodes(G, mapping)
这将创建一个字典mapping其键是旧标签,其值是新标签(在字典理解中读取)。 新标签的排序由G.nodes()返回值(您无法控制)的顺序给出。 新图H的节点标签已更改。 如果需要特定订单,则需要适当地对G.nodes()进行排
...
问题是你试图在三个元素之间生成一条边。 add_edges_from()函数获取元组列表并在每个元组的两个元素之间创建边。 例如 g = networkx.Graph()
g.add_edges_from([(1,2), (3,4)])
会产生两条边:一条在节点1和2之间,另一条在节点3和4之间。 zip函数在您的代码中通过集合user.user1和user.user2 ,返回这样的元组列表(确切地说,它是一个zip对象,但在这种情况下,它被视为与列表完全相同)。 在您的示例中,列表将如下所示:
...
原因是图表是无向的。 igraph和networkx处理I - J平局和J - I networkx对待。 panda.intersection只会处理完全匹配(即数据帧A中的第1列与数据帧B中的第1列匹配,数据帧A中的第2列与数据帧B中的第3列匹配)。 library(igraph); library(dplyr)
set.seed(1034)
g1
set.seed(1646)
g2
...
你看到的问题是由 IPList = [[] for i in range (20)]
当len(pkts)小于20时,这导致parsePcap()返回一个带有空列表的序列列表或者末尾的列表: parseOutput = [
('172.16.254.128', '216.58.208.206'),
...
('172.16.254.128', '216.58.208.227'),
[], #
]
将p
...
该错误在绘图节点的功能中。 p.keys()值必须放在nodelist和node_color的列表中,否则它不起作用。 所以正确的是: nx.draw_networkx_nodes(G,pos,nodelist=list(p.keys()),node_size=80,node_color=list(p.values()), cmap=plt.cm.Reds_r)
plt.axis('off')
plt.show()
The error is in the function for drawing
...
编辑2017年11月请注意,这是在networkx 2.0发布之前编写的。 有一个迁移指南,用于将1.x代码更新为2.0代码(特别是使其兼容) 这是一个简单的递归算法。 它假设最多只有一个父母。 如果某些东西没有父母,那就是根。 否则,它返回其父节点的根。 def find_root(G,node):
if G.predecessors(node): #True if there is a predecessor, False otherwise
root = find_
...
您正在使用networkx的2.0版本。 从使用G.degree()的词典变为使用类似字典(但不是dict)的DegreeView 。 请参阅本指南 。 要获得列表中的学位,您可以使用列表理解 : degrees = [val for (node, val) in G.degree()]
You are using version 2.0 of networkx. Which changed from using a dict for G.degree() to using a dict-lik
...
这篇博客介绍了如何在Python中使用Networkx库获取无向图中每个节点的度数。作者遇到的问题是在迭代DegreeView对象时尝试调用.values()方法,但发现该对象没有这个属性。解决方案是使用列表推导式来获取节点的度数。
588

被折叠的 条评论
为什么被折叠?



