Iterating over the edgelist

Asked by Raj Kumar Pan

I have a graph say:

l = [("a", "b", 3.0), ("c", "d", 4.0), ("a", "c", 5.0)]

ids=UniqueIdGenerator()
edgelist = [(ids[u], ids[v]) for u, v, _ in l]
weights = [w for _, _, w in l]
g=Graph(edgelist, vertex_attrs=dict(name=ids.values()), edge_attrs=dict(weight=weights))

To iterate over the edge list and print the nodes and weights I did

inv_map = {ids[k]:k for k in ids.values()}
for i, j in g.get_edgelist():
    print inv_map[i], inv_map[j], g[i,j]

Is there a way to iterate over the edgelist without creating the inverse map?

Many Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
igraph Edit question
Assignee:
Tamás Nepusz Edit question
Solved by:
Tamás Nepusz
Solved:
Last query:
Last reply:
Revision history for this message
Best Tamás Nepusz (ntamas) said :
#1

Sure, there is:

for i, j in g.get_edgelist():
    print g.vs[i]["name"], g.vs[j]["name"], g[i,j]

Alternatively, you can simply iterate over the edge sequence of the graph and avoid creating the whole edgelist just for the sake of iteration:

for edge in g.es:
    print g.vs[edge.source]["name"], g.vs[edge.target]["name"], g[i,j]

Revision history for this message
Raj Kumar Pan (rajkrpan) said :
#2

Thanks Tamás Nepusz, that solved my question.