簡體   English   中英

如何從字典中添加igraph對象中的邊

[英]How to add edges in an igraph object from a dictionary

我正在使用字典來表示一組邊。 我正在使用字典的鍵來表示邊緣,並使用值來表示權重。 字典目前看起來像這樣:

{(0, 1): 2, (1, 2): 6, (0, 2): 3}

我試試這個:

edges, weights = [], []
for edge, weight in dict_edges.items():
    edges += [edge]
    weights.append(weight)

g.add_edges(edges)
g.es["weight"] = weights

但是,如果有更快的方式或更清潔,我不會這樣做。

任何人有任何建議如何改善我的新?

你做的很完美; 也許for循環可以用zip調用替換。 如果您使用的是Python 2.x ::

from itertools import izip
edges, weights = izip(*dict_edges.iteritems())
g = Graph(edges, edge_attrs={"weight": weights})

如果您使用的是Python 3.x ::

edges, weights = zip(*dict_edges.items())
g = Graph(edges, edge_attrs={"weight": weights})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM