I have a graph with positive and negative edges in networkx. I want to find all the negative edges and reverse them. I wrote the program below to find and delete the negative edges but i get an iteration error. Here is the code:
for edge in G.edges():
sign = G.get_edge_data(edge[0], edge[1])['sign']
if sign == -1:
G.remove_edge(edge[0], edge[1])
G.add_edge(edge[1], edge[0])
How can i iterate through all edges and reverse all the negative edges?
Changing elements of the iteration while iterating is evil ;o)
thingsToChange = []
for edge in G.edges():
sign = G.get_edge_data(edge[0], edge[1])['sign']
if sign == -1:
thingsToChange.append(edge)
Changing them afterwards is fine:
for things in thingsToChange:
G.remove_edge(edge[0], edge[1])
G.add_edge(edge[1], edge[0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With