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

问题描述

我有两个边的节点字典,如下所示:

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