使用 igraph python 从文件中读取关系

Read relations from file using igraph python

我有一个 sample.txt 文件包含这个:

a b c
b d a h
a c

,其中每条线表示每个节点之间有一条有向边,因此使用上面的数据将建立这些关系:

a->b->c
b->d->a->h
a->c

有什么方法可以使用 python igraph 正确导入文件吗?

像这样:

def iterpairs(iterable):
    prev = None
    for item in iterable:
        if prev is not None:
            yield prev, item
        prev = item

edges = []
for line in open("sample.txt", "r"):
    parts = line.strip().split()
    edges.extend(iterpairs(parts))

g = Graph.TupleList(edges)