Reputation: 413
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))
Upvotes: 12
Views: 18986
Reputation: 1633
The accepted answer is now slightly outdated: the edges_iter
method is deprecated.
The edges
attribute can be used as a method or a property. (It’s actually an EdgeView
value, which is iterable, and callable)
Now you can iterate over all edges this way:
for u,v in G.edges:
print(u,v)
or, like before, with data
for u, v, d in G.edges(data=True):
print(f"({u}, {v}) {d=}")
Upvotes: 4
Reputation: 25319
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}
Upvotes: 19