networkx set node attributes from python dictionary -


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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -