简体   繁体   English

无法从列表添加networkx节点和链接

[英]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. 我有要测试添加到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. 您的代码,即node_listlink_list不正确。 An item in the node_list should be a tuple with the form (node_label, attrib_dict). node_list中的一项应为元组,其形式为(node_label,attrib_dict)。 Similarly, items in the link_list (from_node, to_node, attrib_dict). 同样,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'}})])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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