简体   繁体   中英

form edge between nodes by attribute look up in igraph in python

I have a graph g and it has a 'name' attribute associated with each node. I would like to call two nodes by their name attributes and create an edge between them.

from igraph import *
g = Graph(4)

print g
IGRAPH UN-- 4 0 --
+ attr: name (v)

g.vs["name"]
[10, 39, 76, 6]

However, when I try something like

g.add_edge(g.vs[76], g.vs[39]) 

I get a vertex index out of range. I would be happy with a solution that calls a node by it's index if I could match name to index position as long as it's fast, because my graph can get quite large and I do not know how slow that will become.

Ideally, I would just look up nodes by their 'name' attributes and form an edge between them.

You are indexing the names based on their position in the name vector, as you probably guessed. You can match the name to its position in the name vector:

v1_position = g.vs['name'].index(76)
v2_position = g.vs['name'].index(39)

g.add_edge(v1_position, v2_position)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM