Networkx neighbor set not printing

前端 未结 3 1027
遇见更好的自我
遇见更好的自我 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:03

    You can make method for that like,

    def neighbors(G, n):
    """Return a list of nodes connected to node n. """
    return list(G.neighbors(n))
    

    And call that method as:

    print(" neighbours = ", neighbors(graph,'5'))
    

    Where 5 is the node in a graph and

    graph = nx.read_edgelist(path, data = (('weight', float), ))
    

    and path variable contains dataset file path value where data is in more numbers of nodes and edges.

提交回复
热议问题