neighbors = G.neighbors_iter AttributeError:'Graph'对象没有属性'neighbors_iter'

时间:2017-03-27 18:41:46

标签: python networkx

import networkx as nx 
def core_number(G):
    nodes=list(G.nodes())
    if G.is_multigraph():
        raise nx.NetworkXError('MultiGraph and MultiDiGraph types not supported.')

    if G.number_of_selfloops()>0:
        raise nx.NetworkXError('Input graph has self loops; the core number is not defined.','Consider using G.remove_edges_from(G.selfloop_edges()).')

    if G.is_directed():
        def neighbors(v):
            return itertools.chain.from_iterable([G.predecessors_iter(v), G.successors_iter(v)])
    else:
        neighbors=G.neighbors_iter
    return neighbors

我正在使用python3和networkx-2.0。上面的代码给出了以下错误:

   neighbors=G.neighbors_iter()
   AttributeError: 'Graph' object has no attribute 'neighbors_iter'

1 个答案:

答案 0 :(得分:2)

NetworkX 2.0打破了API的变化。为1.x编写的代码需要migration。例如,引用迁移指南,

  

用于返回容器的方法现在返回迭代器,并且已删除返回迭代器的方法。

neighbors_iter不再是一件事; neighbors现在就完成了这项工作。同样,predecessors_itersuccessors_iter也不存在。

(另外,NetworkX 2.0还没有出现。你可能不应该使用开发分支。)