在Python的networkx模块中,“Graph”对象没有“nodes_iter”属性。

17

我在Python 2.7中使用networkx模块编写了下面的函数,但出现了错误。

for H in networkx.connected_component_subgraphs(G):
    bestScore = -1.0
    for n, d in H.nodes_iter(data=True):
        if d['Score'] > bestScore:
            bestScore = d['Score']
            bestSV = n
    if bestSV is not None:
        selectedSVs.add(bestSV)

错误:

Traceback (most recent call last):
File "cnvClassifier.py", line 128, in <module>
for n, d in H.nodes_iter(data=True):
AttributeError: 'Graph' object has no attribute 'nodes_iter'

有人知道出了什么问题吗?

2个回答

23

您可能正在使用预发布版本的networkx-2.0,该版本已删除nodes_iter()方法,并现在提供具有相同功能的nodes()方法。 有关networkx-2.0更改的详细信息,请参见此处


3
现在应该就是这样了:https://networkx.github.io/documentation/stable/release/release_2.0.html - maciej
同时,链接到从1.x迁移到2.0的迁移指南 - kirogasa

10

以防链接再次更改,我会在这里发布实际解决方案以供日后参考。

从NetworkX 2.0开始,您应该将以下代码行更改为:

for n, d in H.nodes_iter(data=True):

至:

for n, d in list(H.nodes(data=True)):

1
我遇到了这个错误:TypeError: unhashable type: 'dict'。 - Mohammad Heydari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接