Issue
I'm currently using the version of NetworkX preinstalled with Spyder 3.3.6 (NetworkX 2.3), and when I add attributes to a node within a for loop, the changes are not reflected until I exit out of the loop. Here is an excerpt from my code:
wordgraph = nx.DiGraph()
for i in range(len(lines)):
for j in range(len(curline)):
...
wordgraph.add_node(newid, name=cur, attribute=wordstat)
print("contents of newly added", newid, ":", wordgraph[newid])
...
print(nx.get_node_attributes(wordgraph, 'attribute'))
The first print outputs something like this:
contents of newly added 34 : {}
whereas the second print outputs a populated dictionary like
{-1: 'start', 9999: 'end', 2: None, 3: 'r', 4: 'n', 5: 'n', 6: 'n', 7: 'a', 8: None, 9: 'n', 10: None, 11: 'n', 12: None, 13: None, 14: 'n', 15: 'n', 16: 'a', 17: None, 18: 'n', 19: None, 20: 'n', 21: 'n', 22: 'a', 23: None, 24: 'n', 25: None, 26: None, 27: 'a', 28: 'n', 29: None, 30: 'n', 31: 'n', 32: 'n', 33: 'a', 34: 'r', 35: None, 36: 'n'}
I need the node attributes to update immediately when each node is created, but I can't tell what I'm doing wrong for NetworkX to put off adding attributes until the end. What can I do to fix this?
Solution
wordgraph[newid]
is not the dictionary of attributes of newid
. It is a collection of neighbors of newid
.
Instead have it print wordgraph.node[newid]
.
Answered By - Joel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.