Sharmin Khan
Sharmin Khan

Reputation: 317

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

I am working with python3 and networkx-2.0. The above code give the following error:

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

Upvotes: 1

Views: 3257

Answers (1)

user2357112
user2357112

Reputation: 280837

NetworkX 2.0 makes breaking API changes. Code written for 1.x will need migration. For example, quoting the migration guide,

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

neighbors_iter isn't a thing any more; neighbors does that job now. Similarly, predecessors_iter and successors_iter don't exist either.

(Also, NetworkX 2.0 isn't even out yet. You probably shouldn't be using the development branch.)

Upvotes: 5

Related Questions