i'm trying write general function creates graph takes list of nodes , edges. each node, there's set of default attributes, , set of optional attributes. optional attributes can anything, i'm thinking use dictionary store them. however, looks add_node() doesn't seems accept variable keyword. given below code snippet,
import networkx nx optional_attrs = {'ned':1, 'its':'abc'} g = nx.graph() g.add_node('node1') k, v in optional_attrs.iteritems(): g.add_node('node1', k=v) print g.node(data=true)
i
nodedataview({'node1':{'k':'abc'}})
instead of,
nodedataview({'node1':{'ned':1, 'its':'abc'}})
i wonder possible achieve that?
in general in python if want use dict
provide keyword arguments function prepend dict **
.
g.add_node('node1', **optional_attrs)
you can add/change node attributes after adding nodes:
g.add_node('node1') g.nodes['node1'].update(optional_attrs)
Comments
Post a Comment