I'm trying to write a general function which creates a graph that takes a list of nodes and edges. For each node, there's a set of default attributes, and a set of optional attributes. As the optional attributes can be anything, I'm thinking to use a dictionary to store them. However, it looks like add_node() doesn't seems to accept a variable as keyword. Given the below code snippet,
import networkx as nx
optional_attrs = {'ned':1, 'its':'abc'}
g = nx.Graph()
g.add_node('node1')
for k, v in optional_attrs.iteritems():
g.add_node('node1', k=v)
print g.node(data=True)
I get
NodeDataView({'node1':{'k':'abc'}})
Instead of,
NodeDataView({'node1':{'ned':1, 'its':'abc'}})
I wonder it is possible to achieve that?
g.add_node('node1', attr_dict=optional_attrs)should do the job. You should read the documentation: networkx.github.io/documentation/stable/reference/generated/… - Simon Frommekthe name of the argument you are passing toadd_nodewill always be just the string"k". - Simon Fromme