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

时间:2019-07-15 08:50:30

标签: python json rest api networkx

我有要测试添加到networkx的节点和链接的列表。

不幸的是,我遇到错误,无法绘制。 这是节点和链接的列表

节点:-

[{'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'}]

链接:-

[{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'}}]

对于初始测试,我在下面添加节点和链接

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

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

执行代码时...出现以下错误

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

提供帮助。谢谢


我真的希望有人可以在这方面帮助我...我认为这只是一个简单的步骤...但是我还没有做好...谢谢

任何人都可以帮助我..谢谢

1 个答案:

答案 0 :(得分:0)

您的代码node_listlink_list不正确。 node_list中的一项应为元组,其形式为(node_label,attrib_dict)。同样,link_list中的项目(from_node,to_node,attrib_dict)。这是您可以尝试的代码:

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)

要检查节点的数据是否完整:

G.nodes(data=True)

响应应为:

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'}})

要检查边缘的数据是否完整:

G.edges(data=True)

输出将类似于以下内容:

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'}})])