我使用以下代码创建图表并添加节点。
g = Graph()
g.add_vertices(3)
g.vs["name"] = ["ABC","ABCD","ACD"]
我想从图中选择顶点名称包含' AB'的顶点。从上图中,它应该返回" ABC"和" ABCD"。有没有办法做到这一点?
答案 0 :(得分:2)
由于您尚未指定图表将包含的节点数,因此我假设您不需要进行缩放。您可以使用List Comprehension:
a = ["ABC","ABCD","ACD"] #List of vertices
#or in your case a = g.vs['name']
b = ["AB"] #Pattern to be matched
match =[vert for patt in b for vert in a if patt in vert]
print match
#['ABC', 'ABCD']