通过顶点ID获取边权重
我想获取边的权重,但遇到了一些问题。
首先,我试了这个:
from igraph import *
g = Nexus.get("karate")
weight = g.es[g.get_eid(vertex, neighbor)]['weight']
但是,如果边不存在,igraph会返回错误。我希望,比如说,只返回-1。
其次,IGRAPH有没有比这个更高效的操作或函数呢?
2 个回答
2
看起来在 get_eid
方法中有一个关键词:
get_eid(v1, v2, directed=True, error=True)
@param error: if C{True}, an exception will be raised when the
given edge does not exist. If C{False}, -1 will be returned in
that case.
所以在你的情况下,这意味着:
weight = g.es[g.get_eid(vertex, neighbor, error=False)]['weight']
0
我喜欢这个:
from igraph import *
g = Nexus.get("karate")
weight = g.es.select(_source=0, _target=1)['weight']
print weight
当没有边的时候,它会返回一个空的集合。