简体   繁体   English

如何在igraph python中删除具有特定权重的所有边缘?

[英]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 ? 我是Python的新手,所以很抱歉,如果这很简单,我想删除weight zero所有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: graph_like.delete_edges("weight"==0)不起作用,因为它的评估如下:

  1. Python evaluates "weight" == 0 , which is a comparison of the string "weight" with the number zero. Python评估"weight" == 0 ,它是字符串"weight"与数字零的比较。 The result of this comparison is False , a value of type bool . 比较结果为False ,它是bool类型的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. 然后将上述表达式的结果馈入graph_like.delete_edges() ,该graph_like.delete_edges()期望将边缘ID列表作为其第一个参数。 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. 由于边缘ID是整数,因此它将False转换为整数,使其等于零,然后igraph删除ID为零的边缘。

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. 其中graph_like.es表示图的边缘序列(即所有边缘按顺序排列),其select()方法基于某些条件限制边缘序列。 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). 在这里, weight=0select()的关键字参数(请注意, weight0之间只有一个等号,而weight在这里不是字符串)。 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: 然后,您可以简单地调用此边缘序列的delete()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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