Networkx邻居设置不打印 [英] Networkx neighbor set not printing

查看:122
本文介绍了Networkx邻居设置不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的networkx代码有点问题. 我正在尝试从图中的节点中查找所有邻居,但是....

I have a little problem with my networkx code. I am trying to find all the neighbors from a node in a graph, but....

neighbor = Graph.neighbors(element)
print(neighbor)

输出:

<dict_keyiterator object at 0x00764BA0>

我应该得到的不是所有邻居...我的一个朋友(正在使用较旧版本的networkx)没有收到此错误,他的代码完全相同并且可以正常工作.

谁能帮我?不能降低我的networkx的等级.

Instead of all the neighbors I am supposed to get... A friend of mine, who is using an older version of networkx does not get this error, his code is exactly the same and works perfectly.

Can anyone help me? Downgrading my networkx is not an option.

这是我完整的代码

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))

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

This is the graph I am using: http://politicalmashup.nl/new/uploads/2013/09/macbethcorrected.graphml

推荐答案

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

From networkx 2.0 onwards, Graph.neighbors(element) returns an iterator rather than a list.

要获取列表,只需应用list

To get the list, simply apply list

list(Graph.neighbors(element))

或使用列表理解:

neighbors = [n for n in Graph.neighbors(element)]

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

The first method (first mentioned by Joel) is the recommended method, as it's faster.

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

这篇关于Networkx邻居设置不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆