简体   繁体   中英

Not able to add networkx nodes and links from a list

I have list of nodes and links that I test to add onto networkx.

Unfortunately I'm getting error and not able to draw it. This is the list of nodes and links

Nodes:-

[{'id': u'openflow:1'}, {'id': u'host:00:00:00:00:00:01'}, {'id': 
u'openflow:2'}, {'id': u'host:00:00:00:00:00:02'}]

Links:-

[{u'link-id': u'host:00:00:00:00:00:01/openflow:1:1', u'destination': {u'dest-node': u'openflow:1', u'dest-tp': u'openflow:1:1'}, u'source': {u'source-tp': u'host:00:00:00:00:00:01', u'source-node': u'host:00:00:00:00:00:01'}}, {u'link-id': u'openflow:2:1/host:00:00:00:00:00:02', u'destination': {u'dest-node': u'host:00:00:00:00:00:02', u'dest-tp': u'host:00:00:00:00:00:02'}, u'source': {u'source-tp': u'openflow:2:1', u'source-node': u'openflow:2'}}, {u'link-id': u'openflow:1:2', u'destination': {u'dest-node': u'openflow:2', u'dest-tp': u'openflow:2:2'}, u'source': {u'source-tp': u'openflow:1:2', u'source-node': u'openflow:1'}}, {u'link-id': u'openflow:2:2', u'destination': {u'dest-node': u'openflow:1', u'dest-tp': u'openflow:1:2'}, u'source': {u'source-tp': u'openflow:2:2', u'source-node': u'openflow:2'}}, {u'link-id': u'openflow:1:1/host:00:00:00:00:00:01', u'destination': {u'dest-node': u'host:00:00:00:00:00:01', u'dest-tp': u'host:00:00:00:00:00:01'}, u'source': {u'source-tp': u'openflow:1:1', u'source-node': u'openflow:1'}}, {u'link-id': u'host:00:00:00:00:00:02/openflow:2:1', u'destination': {u'dest-node': u'openflow:2', u'dest-tp': u'openflow:2:1'}, u'source': {u'source-tp': u'host:00:00:00:00:00:02', u'source-node': u'host:00:00:00:00:00:02'}}]

for initial testing I add nodes and links below

graph.add_nodes_from(node_list)
graph.add_edges_from(link_list)

nx.draw(graph, with_labels=True)
plt.show()

When execute the code...getting error below

graph.add_nodes_from(node_list)
  File "/usr/local/lib/python2.7/dist-packages/networkx/classes/graph.py", line 560, in add_nodes_from
    nn, ndict = n
ValueError: need more than 1 value to unpack

Appreciate help. Thanks


I really hope someone could help me on this...I think it just a simple step...but I'm yet to get it right...thanks

Anybody could help me..Thanks

Your code, namely, node_list and link_list are not correct. An item in the node_list should be a tuple with the form (node_label, attrib_dict). Similarly, items in the link_list (from_node, to_node, attrib_dict). Here is a code that you can try:

import networkx as nx

# there are 4 nodes with data
node_list = [(1,{'id': u'openflow:1'}), \
             (2,{'id': u'host:00:00:00:00:00:01'}), \
             (3,{'id': u'openflow:2'}), \
             (4,{'id': u'host:00:00:00:00:00:02'})]

# only 2 edges will be added for demo purposes
# edge attributes: from node 1 to node 2
d12 = {u'link-id': u'host:00:00:00:00:00:01/openflow:1:1', 
u'destination': {u'dest-node': u'openflow:1', u'dest-tp': u'openflow:1:1'}, 
u'source': {u'source-tp': u'host:00:00:00:00:00:01', u'source-node': u'host:00:00:00:00:00:01'}}

# edge attributes: from node 2 to node 3
d23 = {u'link-id': u'openflow:2:1/host:00:00:00:00:00:02', 
 u'destination': {u'dest-node': u'host:00:00:00:00:00:02', u'dest-tp': u'host:00:00:00:00:00:02'}, 
 u'source': {u'source-tp': u'openflow:2:1', u'source-node': u'openflow:2'}}

# list of all edges with accomp attributes
link_list = [(1, 2, d12), (2, 3, d23)]

G = nx.Graph()
G.add_nodes_from(node_list)
G.add_edges_from(link_list)

To check the nodes if their data are in tact:

G.nodes(data=True)

The response should be:

NodeDataView({1: {'id': 'openflow:1'}, 2: {'id': 'host:00:00:00:00:00:01'}, 3: {'id': 'openflow:2'}, 4: {'id': 'host:00:00:00:00:00:02'}})

To check the edges if their data are in tact:

G.edges(data=True)

Output will be similar to this:

EdgeDataView([(1, 2, {'link-id': 'host:00:00:00:00:00:01/openflow:1:1', 'destination': {'dest-node': 'openflow:1', 'dest-tp': 'openflow:1:1'}, 'source': {'source-tp': 'host:00:00:00:00:00:01', 'source-node': 'host:00:00:00:00:00:01'}}), (2, 3, {'link-id': 'openflow:2:1/host:00:00:00:00:00:02', 'destination': {'dest-node': 'host:00:00:00:00:00:02', 'dest-tp': 'host:00:00:00:00:00:02'}, 'source': {'source-tp': 'openflow:2:1', 'source-node': 'openflow:2'}})])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM