Networkx neighbor set not printing

前端 未结 3 1006
遇见更好的自我
遇见更好的自我 2020-12-16 19:42

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         


        
3条回答
  •  隐瞒了意图╮
    2020-12-16 20:09

    As others have noted, in networkx 2.0 neighbors returns an iterator rather than a list. Networkx has provided a guide for migrating code written in 1.x to 2.0. For neighbors, it recommends

    list(G.neighbors(n))
    

    (see Fastest way to convert an iterator to a list). The migration guide provides the example:

    >>> G = nx.complete_graph(5)
    >>> n = 1
    >>> G.neighbors(n)
    
    >>> list(G.neighbors(n))
    [0, 2, 3, 4]
    

提交回复
热议问题