2016-02-09 88 views
3

我想要獲取網絡x圖中兩個節點之間的邊。更具體地說,我想獲得一些與此邊緣相關的數據。我知道先驗這兩個節點是鏈接的。有沒有這樣做的功能?如何獲得兩個節點之間的邊緣?

+0

你是什麼意思是什麼呢?在你知道連接的兩個特定節點之間繪製一條邊,或者搜索具有邊的節點,然後根據這些節點具有的其他屬性來計算一些東西? – Reti43

+0

@ Reti43我的意思是,給定兩個節點'u'和'v',得到連接它們的邊緣。 – becko

+0

類似['get_edge_data()'](https://networkx.github.io/documentation/development/reference/generated/networkx.Graph.get_edge_data.html)?還有['edges()'](https://networkx.github.io/documentation/latest/reference/generated/networkx.Graph.edges.html),但它會返回連接的節點列表。 – Reti43

回答

4

邊緣數據存儲在字典中。要訪問該字典,請使用get_edge_data()

import networkx as nx 
G=nx.Graph() 
G.add_edge(1,2, weight=5) 
G.get_edge_data(1,2) 
> {'weight': 5} 

如果你想通過所有的邊緣進行迭代,你可以使用G.edges(data=True)

H = nx.Graph() 
H.add_edge(2, 3, color = 'red') 
H.add_edge(1, 2, weight = 4) 
for u,v,data in H.edges_iter(data=True): 
    print u, v, data 
> 1 2 {'weight': 4} 
> 2 3 {'color': 'red'} 
相關問題