2013-06-11 39 views
5

我試圖從圖中獲取具有特定屬性的邊,而不使用get_edge_attributes()函數。我需要一個更靈活的方式來做到這一點。我可以得到節點的屬性,但由於我是新的python在邊緣似乎很難解析通過NetworkX圖中的邊線

G = nx.read_graphml("test.graphml") 

for n in G: 
    print "%s\t%s" %(n, G.node[n].get(attr)) 

for (s,d) in G:  # and here is my problem 
    print "%s->%s\t%s" %(s, d, G.edge[s][d].get(attr)) 

回答

6

您可以在所有圖中邊用G.edges()或G.edges_iter()方法來循環。

In [1]: import networkx as nx 

In [2]: G = nx.Graph() 

In [3]: G.add_edge(1,2,weight=7) 

In [4]: G.add_edge(2,3,weight=10) 

In [5]: for u,v,a in G.edges(data=True): 
    print u,v,a 
    ...:  
1 2 {'weight': 7} 
2 3 {'weight': 10} 
+0

謝謝Aric! – geolykos