两点间输出所有程序并排序

怎么做到输出所有路径后排序呢

import networkx as nx

# 建立带权图
G = nx.Graph()

# 向图中添加边,每条边都有一个权值
G.add_edge('A', 'B', weight=5)
G.add_edge('A', 'C', weight=3)
G.add_edge('A', 'D', weight=2)
G.add_edge('B', 'C', weight=1)
G.add_edge('B', 'D', weight=2)
G.add_edge('C', 'D', weight=4)

# 查找从点 A 到点 B 的所有路径
all_paths = nx.all_simple_paths(G, 'A', 'B')

for path in all_paths:
    print(path)


代码如下,望采纳

import networkx as nx
 
# 建立带权图
G = nx.Graph()
 
# 向图中添加边,每条边都有一个权值
G.add_edge('A', 'B', weight=5)
G.add_edge('A', 'C', weight=3)
G.add_edge('A', 'D', weight=2)
G.add_edge('B', 'C', weight=1)
G.add_edge('B', 'D', weight=2)
G.add_edge('C', 'D', weight=4)
 
# 查找从点 A 到点 B 的所有路径
all_paths = nx.all_simple_paths(G, 'A', 'B')

# 记录路径与权重和
all_paths_with_weight = {}
for path in all_paths:
    weight_sum = 0
    for i in range(len(path)-1):
        A = path[i]
        B = path[i+1]
        weight = G.get_edge_data(A, B)['weight']
        weight_sum += weight
    all_paths_with_weight[str(path)] = weight_sum
    
# 排序
a = sorted(all_paths_with_weight.items(),key = lambda x:x[1],reverse = True)
print(a)

img