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

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

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'

NetworkX 2.0 进行了重大 API 更改。为 1.x 编写的代码将需要 migration。比如引用迁移指南,

Methods that used to return containers now return iterators and methods that returned iterators have been removed.

neighbors_iter 不再是一回事; neighbors 现在做那个工作。同样,predecessors_itersuccessors_iter 也不存在。

(此外,NetworkX 2.0 甚至还没有发布。您可能不应该使用开发分支。)