简体   繁体   中英

how to delete all edges with specific weight in igraph python?

I am new to Python so sorry if this is so simple,I am trying to delete all edges which its weight is zero but with this code I was just able to delete one edge at a time, How to make it in away that it will delete all the edges ?

 ig.summary(graph_like)
IGRAPH D-W- 5390 40276 -- 
graph_like.delete_edges("weight"==0);
ig.summary(graph_like)
IGRAPH D-W- 5390 40275 -- 

thank you in advance

graph_like.delete_edges("weight"==0) does not work because it evaluates as follows:

  1. Python evaluates "weight" == 0 , which is a comparison of the string "weight" with the number zero. The result of this comparison is False , a value of type bool .

  2. The result of the above expression is then fed into graph_like.delete_edges() , which expects a list of edge IDs as its first argument. Since edge IDs are integers, it will cast False into an integer, making it equal to zero, then igraph deletes the edge with ID zero.

Instead of that, you need to select all the edges with zero weight:

graph_like.es.select(weight=0)

where graph_like.es represents the edge sequence of the graph (ie all the edges in order), and its select() method restricts the edge sequence based on certain criteria. Here, weight=0 is a keyword argument of select() (note that there is only a single equals sign between weight and 0 and weight is not a string here). The result of the above expression is the sequence of all the edges that have zero weight. Then you can simply invoke the delete() method of this edge sequence:

graph_like.es.select(weight=0).delete()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM