I have a network of nodes created using python networkx. i want to store information in nodes such that i can access the information later based on the node lab
Additionally, you don't have to just assign the attributes when the node is added. Even after it's been added you can still set them directly.
import networkx as nx
G=nx.Graph()
G.add_edge(1,2)
#see comment below code for recent versions of networkx.
G.node[1]['name'] = 'alpha'
G.node[2]['name'] = 'omega'
G.node[1]['name']
> 'alpha'
Note: In version 2.4+, use G.nodes[] instead of G.node[]. See deprecation notes.
You can also use set_node_attributes (documentation) which will let you set an attribute for multiple nodes at the same time:
edit
#use the next line if it's networkx version 1.x
#nx.set_node_attributes(G, 'cost', {1:3.5, 2:56})
#use the next line if it's networkx version 2.x
#nx.set_node_attributes(G, {1:3.5, 2:56}, 'cost')
#or for something that works for 1.x or 2.x
nx.set_node_attributes(G, values = {1:3.5, 2:56}, name='cost')
G.node[1]['cost']
> 3.5
Similar approaches can be used to set edge attributes.