简体   繁体   English

在igraph中基于源/目标选择边

[英]selecting edges based on source/target in igraph

is there an easy way to select/delete edges based on their source and target in igraph? 有没有一种简单的方法可以根据igraph中的源和目标选择/删除边缘?

what I am using is essentially 我使用的基本上是

g.es["source"] = [e.source for e in g.es]
g.es["target"] = [e.target for e in g.es]    
g.es["tuple"]  = [e.tuple  for e in g.es]        

g.es.select(target=root)

but I feel like there should be a way to do that without storing source/target info twice. 但我觉得应该有一种方法可以做到这一点,而无需两次存储源/目标信息。

Just use _source=whatever and _target=whatever as keyword arguments to select , eg: 只需使用_source=whatever_target=whatever作为关键字参数进行select ,例如:

g.es.select(_source=root)

Alternatively, you can use the incident method of the graph, which gives you a list of edge IDs instead of a filtered EdgeSeq if that is better for your purposes: 或者,您可以使用图表的incident方法,如果更适合您的目的,它会为您提供边缘ID列表而不是过滤后的EdgeSeq

g.incident(root, mode="out")

BTW, for 'tuple', you want to use _between : 顺便说一句,对于'元组',你想使用_between

g.es.find(_between=((source_id,), (target_id,)))

It looks strange - if you use select instead of find , and pass in tuples with multiple indices, you'll actually get a list of edges instead of a single one. 它看起来很奇怪 - 如果你使用select而不是find ,并传入带有多个索引的元组,你实际上会得到一个边列表而不是一个边。 But for a single edge, have to still pass a tuple for start and end. 但是对于单个边缘,还必须通过一个元组来开始和结束。

This is way faster for some reason (like, 3 orders of magnitude faster!) than using a combination of _source and _target , but gives the exact same information. 由于某种原因(比使用_source_target的组合快3个数量级!),这种方式更快,但提供完全相同的信息。

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

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