使用 iGraph 获取 python 中特定节点的邻居

get neighbors of specific node in python using iGraph

我正在尝试获取图中特定节点的邻居。图形看起来像这样

print g

IGRAPH UN-- 6 3 --
+ attr: name (v), position (v)
+ edges (vertex names):
40--115, 116--98, 44--98

g.vs['name]
[116, 40, 44, 115, 98, 116]

我尝试使用以下方法获取 40

的邻居
g.neighbors(g.vs['name'][1])

但我收到以下错误:

InternalError: Error at type_indexededgelist.c:750: cannot get neighbors, Invalid vertex id

我也试过这个,但是得到了不同的错误

g.neighbors('40')

ValueError: no such vertex: '40'

有什么想法吗?

您正在向函数 neighbors 传递一个字符串,但它需要一个整数或 Vertex 对象。尝试:

g.neighbors(g.vs[1])

g.neighbors(1)