通过顶点id获取边缘权重

时间:2014-04-09 20:40:17

标签: python igraph

我想要获得边缘重量,但我遇到了问题。

首先,我试过这个:

from igraph import *
g = Nexus.get("karate")
weight = g.es[g.get_eid(vertex, neighbor)]['weight']

但是,如果边缘不存在igraph返回错误。例如,我想只返回-1

第二,IGRAPH中的操作/功能在计算上比这更有效吗?

2 个答案:

答案 0 :(得分:1)

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']

答案 1 :(得分:0)

我喜欢这个:

from igraph import *
g = Nexus.get("karate")
weight = g.es.select(_source=0, _target=1)['weight']
print weight

当没有边缘时,返回空集。

相关问题