从 python-igraph 获取顶点列表
Getting vertex list from python-igraph
我一直在搜索文档,试图找到一个可以 return 从 iGraph 图中列出顶点但找不到的函数。有谁知道如何从 iGraph 图中获取顶点列表?
property vs
of the igraph.Graph
object refers to its VertexSeq
对象:
g = Graph.Full(3)
vseq = g.vs
print type(vseq)
# <class 'igraph.VertexSeq'>
您还可以根据图表创建一个:
g = Graph.Full(3)
vs = VertexSeq(g)
您可以使用 property 作为迭代器:
g = Graph.Full(3)
for v in g.vs:
# do stuff with v (which is an individual vertex)
您还可以通过
在 python 中尝试更简单的命令版本
G = igraph.Graph.Read("your_inputgraph.txt", format="edgelist", directed=False)
# 将图形读入 igraph
nodes = G.vs.indices
nodes 将是 igraph 中所有节点的列表。
如果您正在寻找 names(如果您使用了顶点的名称),以下代码将按顺序为您提供所有顶点的名称列表:
named_vertex_list = g.vs()["name"]
如果您只查找 indices,那么只需使用 vcount 创建一个范围对象即可得到 indices
vertex_indices = range(g.vcount())
我一直在搜索文档,试图找到一个可以 return 从 iGraph 图中列出顶点但找不到的函数。有谁知道如何从 iGraph 图中获取顶点列表?
property vs
of the igraph.Graph
object refers to its VertexSeq
对象:
g = Graph.Full(3)
vseq = g.vs
print type(vseq)
# <class 'igraph.VertexSeq'>
您还可以根据图表创建一个:
g = Graph.Full(3)
vs = VertexSeq(g)
您可以使用 property 作为迭代器:
g = Graph.Full(3)
for v in g.vs:
# do stuff with v (which is an individual vertex)
您还可以通过
在 python 中尝试更简单的命令版本G = igraph.Graph.Read("your_inputgraph.txt", format="edgelist", directed=False)
# 将图形读入 igraph
nodes = G.vs.indices
nodes 将是 igraph 中所有节点的列表。
如果您正在寻找 names(如果您使用了顶点的名称),以下代码将按顺序为您提供所有顶点的名称列表:
named_vertex_list = g.vs()["name"]
如果您只查找 indices,那么只需使用 vcount 创建一个范围对象即可得到 indices
vertex_indices = range(g.vcount())