Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing through edges in NetworkX graph

I'm trying to get edges that have a certain attribute from a graph without using get_edge_attributes() function. I need a more flexible way of doing it. I can get node attributes but since I'm new at python edges seem difficult

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))
like image 852
geolykos Avatar asked Jun 11 '13 18:06

geolykos


1 Answers

You can use the G.edges() or G.edges_iter() methods to loop over all of the graph edges.

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}
like image 54
Aric Avatar answered Oct 04 '22 17:10

Aric