如何向networkx图添加额外的权重?

问题描述

我正在做一个 Django 项目,在那里我放了 2 个点并得到它们的距离。 但是,有两种类型的重量,我需要获取电缆线的长度和轨道线的长度。 Graph 只能获得一个权重,如何添加额外的权重,例如 weight1、weight2?

def get_distance_length_kls(request,pk1,pk2):
    point1 = get_object_or_404(Point,pk=pk1)
    point2 = get_object_or_404(Point,pk=pk2)
    g = nx.Graph()
    data = []
    content = {'points': None}
    content1 = {"sum_line": 0,"sum_cable": 0}

    for form in Form61KLS.objects.all():
        g.add_edge(form.point1.point,form.point2.point,weight=form.total_length_line)
        #here should be another type of length weight=form.total_length_cable)

    for p in nx.all_simple_edge_paths(g,source=point1.point,target=point2.point):
        finish_total = copy.deepcopy(content)
        finish_total['points'] = p
        data.append(finish_total)
    path = []
    for p in nx.all_simple_paths(g,target=point2.point):
        path.append(p)
    if len(path) > 1:
        for pt in path:
            total = copy.deepcopy(content1)
            path_length = nx.path_weight(g,pt,weight='weight')
            total['sum_line'] = path_length
            data.append(total)
    return JsonResponse(data,safe=False) ```

My output should look like this:

**[
    {
        "points": [
            [
                "UZB/UT/Андиж","UZB/UT/Пахта"
            ],[
                "UZB/UT/Пахта","UZB/UT/Тшк"
            ],[
                "UZB/UT/Тшк","KAZ/ V-Net/Алматы"
            ]
        ]
    },{
        "sum_line": 190.103,"sum_cable": 0
    }
    
]** 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)