您可以使用add_edge
功能,新屬性在MultiDiGraph
添加到現有的邊緣,但需要注意的key
關鍵字(其值必須爲0)。
在我的例子中,我添加了tcv1
屬性的第一個「一」 - >「b」的邊緣(我用你的變量名和我的例子與add_edges_from
圖表創建):
import networkx as nx
router_matrix = nx.MultiDiGraph()
# add two weighted edges ("a" -> "b" and "b" -> "a")
router_matrix.add_edges_from([
("a", "b", {"weight": 0.5}),
("b", "a", {"weight": 0.99})
])
# print list of edges with all data
print(router_matrix.edges(data=True))
tmp_path = [["a", "b"], ["b", "a"]]
traffic_cv = [42, 66]
# add "tcv1" for only the first edge of tmp_path
router_matrix.add_edge(tmp_path[0][0], tmp_path[0][1], key=0, tcv1=traffic_cv[0])
print(router_matrix.edges(data=True))
要添加tcv1
所有邊緣,你可以使用router_matrix.edges()
遍歷所有槽邊緣(注意,這裏我使用G[src_node][dest_node][key][attribute]
語法而不是add_edge
):
# add new attribute to all edges
counter = 0
for src, dest in router_matrix.edges():
router_matrix[src][dest][0]['tcv1'] = traffic_cv[counter]
counter += 1
print(router_matrix.edges(data=True))
來源
2016-11-24 10:10:23
edo