Python NetworkX使用dict添加边缘列表

时间:2017-12-16 09:45:33

标签: python dictionary networkx

我想在GRaph中添加一个边缘元组列表,其中一些属性是字典。文档似乎能够接受字典但我收到错误:

G = nx.MultiDiGraph()

edges = [(34, 1, {'id': '123a'}, {'date': '2017-11-27'}),
 (1, 27, {'id': '123a'}, {'date': '2017-11-27'})]

G.add_edges_from(edges)

我的错误:

TypeError:不可用类型:' dict'

1 个答案:

答案 0 :(得分:1)

尝试在单个字典中添加边缘属性:

G = nx.MultiDiGraph()

edges = [(34, 1, {'id': '123a', 'date': '2017-11-27'}),
 (1, 27, {'id': '123a', 'date': '2017-11-27'})]

G.add_edges_from(edges)
print(G[34][1][0]['id'])

输出中:

'123a'