gpt4 book ai didi

python - 通过顶点id获取边权重

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:05 24 4
gpt4 key购买 nike

我想执行获取边缘权重,但遇到问题。

首先,我尝试了这个:

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

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

第二,有比 IGRAPH 的计算效率更高的操作/函数吗?

最佳答案

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

关于python - 通过顶点id获取边权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com