Best practice networkx get edge attribute value while iterating around edges

Given a list of edges (or generator). What is the most readable way to define boundaries with a specific attribute value? For example, all edges with "edge_type" from "foo"?

Currently my code looks like this:

for edge in nx_graph.out_edges(my_node):
   edge_type = nx_graph[edge[0]][edge[1]]['edge_type']
   if edge_type == 'foo':
       ...

      

Due to the many parentheses, this is not very easy to read ...

A slightly more readable approach:

for edge in G.edges_iter(data=True):
    if edge[2]['edge_type']=='foo':
        ...

      

However, it is still not very clear (especially [2]

). Also, I'm not sure how to use it without_edges()

+3


source to share


1 answer


Here option



for edge in ((u,v,data) for u,v,data in G.edges_iter(data=True) if data['edge_type']=='foo'): 
    ...

      

+3


source







All Articles