Networkx邻居设置不打印

Joe*_*oep 6 graph nodes networkx python-3.x

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

neighbor = Graph.neighbors(element)
print(neighbor)
Run Code Online (Sandbox Code Playgroud)

输出:

<dict_keyiterator object at 0x00764BA0>
Run Code Online (Sandbox Code Playgroud)

而不是我应该得到的所有邻居...我的朋友,使用旧版本的networkx没有得到这个错误,他的代码是完全相同的,并且工作完美.

谁能帮我?降级我的networkx不是一个选择.

编辑:

这是我的完整代码

Graph = nx.read_graphml('macbethcorrected.graphml')    
actors = nx.nodes(Graph)

for actor in actors:
    degree = Graph.degree(actor)
    neighbor = Graph.neighbors(actor)
    print("{}, {}, {}".format(actor, neighbor, degree))
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的图表:http: //politicalmashup.nl/new/uploads/2013/09/macbethcorrected.graphml

Fab*_*ing 9

从networkx 2.0开始,Graph.neighbors(element)返回迭代器而不是列表.

要获取列表,只需申请 list

list(Graph.neighbors(element))
Run Code Online (Sandbox Code Playgroud)

或使用列表理解:

neighbors = [n for n in Graph.neighbors(element)]
Run Code Online (Sandbox Code Playgroud)

第一种方法(由Joel首先提到)是推荐的方法,因为它更快.

参考:https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.neighbors.html