我拥有什么:networkX中的多图H。两个节点'0'和'1'。现有边e1 =(0,1)。
我想要什么:在节点0和1之间添加第二个新边e2。
问题:当我在0和1之间添加新边e2时,e1将更新为e2的新值(属性),并且不会添加e2。在0和1
之间始终存在单个边我的示例代码:
H=nx.MultiGraph()
H=nx.read_gml('my_graph.gml')
如果我打印H的所有边缘,我正确地拥有:
for i in H.edges(data=True):
print i
>>>>>(0, 1, {}) #this is ok
现在我使用关键字attribut:
为e2 =(0,1)添加一条新边H.add_edge(0,1,key=1,value='blue')
但如果我打印H的所有边缘:
for i in H.edges(data=True):
print i
>>>>>(0, 1, {'key': 1, 'value': 'blue'}) #this is error e1 was updated instead add of e2
如您所见,第二个边缘已更新第一个边缘,但是e2添加了指定的键,不同的形式为e1(默认为0)。
我怎样才能避免这个问题? 添加边e2后我想要此结果:
for i in H.edges(data=True):
print i
>>>>>(0: 0, 1, {}, 1: 0,1,{'value': 'blue'} ) #this is correct
答案 0 :(得分:2)
您没有多图,因此您要替换边而不是添加新边。 使用
H=nx.MultiGraph(nx.read_gml('my_graph.gml'))