network programming - Creating key:attribute pairs in networkx for Python -


i working on creating graph method analyzing images using pixels nodes in python. using networkx graph support(documentation here: https://networkx.github.io/documentation/latest/index.html ) take example:

new=np.arange(256) g=nx.graph() x in new:     g.add_node(x) h=g.order() print h 

as expected, 256 nodes created. now, create node:attribute pairs based on array, namely:

newarray=np.arange(256) x in new:     g.add_node(x)     nx.set_node_attributes(g, 'value' newarray[x]) 

with addition of line, hoping first node of newarray assigned first node of g. however, rather, values of g assigned last value of newarray. namely, 256. how can add attribute pairs each node, element element?

you need pass in dictionary third parameter set_node_attribute, 1 that's aligned graph. see if code need:

import numpy np import networkx nx  array1 = np.arange(256) array2 = np.arange(256) * 10  g = nx.graph()  valdict = {}  x in array1:     g.add_node(x)     valdict[x] = array2[x]  nx.set_node_attributes(g, 'value', valdict)  in array1:     print g.nodes()[i], g.node[i]['value'] 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -