Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Networkx邻居集未打印_Python 3.x_Graph_Nodes_Networkx - Fatal编程技术网

Python 3.x Networkx邻居集未打印

Python 3.x Networkx邻居集未打印,python-3.x,graph,nodes,networkx,Python 3.x,Graph,Nodes,Networkx,我的networkx代码有点问题。 我试图从图中的一个节点中找到所有的邻居,但是 neighbor = Graph.neighbors(element) print(neighbor) 产出: <dict_keyiterator object at 0x00764BA0> 这是我正在使用的图表: 从networkx 2.0开始,图形。邻居(元素)返回迭代器而不是列表 要获取列表,只需应用list list(Graph.neighbors(element)) 或使用列表理解: n

我的networkx代码有点问题。 我试图从图中的一个节点中找到所有的邻居,但是

neighbor = Graph.neighbors(element)
print(neighbor)
产出:

<dict_keyiterator object at 0x00764BA0>
这是我正在使用的图表:

从networkx 2.0开始,
图形。邻居(元素)
返回迭代器而不是列表

要获取列表,只需应用
list

list(Graph.neighbors(element))
或使用列表理解:

neighbors = [n for n in Graph.neighbors(element)]
第一种方法(首先提到)是推荐的方法,因为它更快


参考:

正如其他人所指出的,在networkx 2.0
中,邻居
返回迭代器而不是列表。Networkx提供了从1.x到2.0的书面版本。对于
邻居
,它建议

list(G.neighbors(n))
(见附件)。《迁移指南》提供了以下示例:

>>> G = nx.complete_graph(5)
>>> n = 1
>>> G.neighbors(n)
<dictionary-keyiterator object at ...>
>>> list(G.neighbors(n))
[0, 2, 3, 4]
>G=nx.完整图(5)
>>>n=1
>>>G.邻居(n)
>>>名单(G.n)
[0, 2, 3, 4]

您可以为类似的内容创建方法

def neighbors(G, n):
"""Return a list of nodes connected to node n. """
return list(G.neighbors(n))
并将该方法称为:

print(" neighbours = ", neighbors(graph,'5'))
其中5是图中的节点,并且

graph = nx.read_edgelist(path, data = (('weight', float), ))

path变量包含数据集文件路径值,其中数据包含更多的节点和边。

您能给出一个值吗?还有,你使用的是什么networkx版本?为你编辑了我的帖子,你能用这个吗?谢谢。在networkx 1.11(Python 3.5)上似乎适合我。您使用哪个版本?我正在使用networkx 2.0…我建议您阅读---这是关于将代码从networkx 1.x修改为2.0的指南
graph = nx.read_edgelist(path, data = (('weight', float), ))