3

I am trying to find all neighbors of a given node in a defined graph by using G.neighbors() function from NetworkX. I am setting my graph structure of the directed graph as follows:

import networkx as nx

#Define network structure
G=nx.DiGraph()
G.add_edge('x','a', dependency=0.4)
G.add_edge('x','b', dependency=0.6)
G.add_edge('a','c', dependency=0.9)
G.add_edge('b','c', dependency=0.3)
G.add_edge('b','d', dependency=0.7)
G.add_edge('d','e', dependency=0.8)
G.add_edge('c','y', dependency=0.4)
G.add_edge('e','y', dependency=0.2)
G.add_node('x', value=20)
G.add_node('a', value=15)
G.add_node('b', value=35)
G.add_node('c', value=10)
G.add_node('d', value=15)
G.add_node('e', value=5)
G.add_node('y', value=15)

Now, I am trying to find all node neighbors of e.g. the node 'b', which should be 'c' and 'd'. I am using the following function:

print(G.neighbors('b'))

Now, Python throws out no error message but the following statement:

<dict_keyiterator object at 0x000001561D812950>
Process finished with exit code 0

As you can see, it is not throwing out the neighbors of G.node('b') but this message. Can someone help me with this?

CDJB
  • 14,043
  • 5
  • 29
  • 55
Dominik U
  • 95
  • 2
  • 8
  • After checking, this question already has been answered. https://stackoverflow.com/questions/47161158/networkx-neighbor-set-not-printing – Dominik U Feb 13 '20 at 09:58

1 Answers1

3

This is because the graph.neighbors() function returns an iterator object. This can be viewed as a list as we would normally view iterators:

>>> list(G.neighbors('b'))
['c', 'd']
CDJB
  • 14,043
  • 5
  • 29
  • 55