我试图从列表中添加边:
for first in G.nodes():
G_dict = copy.copy(G.node[first])
sorted_G = sorted(G_dict.iteritems(), key = op.itemgetter(1), reverse = True)
for number in range(0, close_limit):
edge_temp.append((first,sorted_G[number][0]))
print(edge_temp)
G.add_edges_from(edge_temp)
当我打印 edge_temp 时,效果很好。但是,当我尝试使用该功能G.add_edges_from()
时,错误是:
Traceback (most recent call last):
File "C:\Python27\Wireless\rd_pc_contact1.py", line 95, in <module>
G.add_edges_from(edge_temp)
File "C:\Python27\lib\site-packages\networkx-
1.8.1py2.7.egg\networkx\classes\graph.py", line 785, in add_edges_from
datadict=self.adj[u].get(v,{})
KeyError: 0
我无法理解发生错误的原因。你能清楚地解释错误吗?