通过字典向边添加特定权值



我有两个节点字典,它们的边如下:

all_nodes_edges = {20101: (20102, 20201), 20102: (20101, 20103), 20103: (20102, 20104)}
nodes_with_weights =  {20101: 20201, 20119: 20219, 20201: (20301, 20101)}

我用下面的代码创建了图和图中所有边的默认权重:

g = nx.Graph(all_nodes_edges)
nx.set_edge_attributes(g, 1, 'weight')

我试图使用nodes_with_weights字典在特定边缘上创建2的权重。我如何做到这一点?我是否需要遍历字典或者我是否可以使用特定的nx函数?

对不起,我对图表有点陌生。

我终于想通了!

for u,v,a in g.edges(data=True):
if u and v in nodes_with_weights:
a = 2

最新更新