![](/img/trans.png)
[英]How to add edges that are conditional to object attribute using igraph and python
[英]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.