如何根据起点和终点对路径的边缘进行排序?
How to sort edges of path based on start and end point?
我对每辆卡车的路径有疑问。从我的模型中,我得到每条卡车路径的输出都是错误的。我想根据起点和终点对我的边列表进行排序。
trucks = {0: (10, 1),1:(7,1),2: (3, 10),3:(7,4)} # truck_number:(start_point, end_point)
我需要更改的输入:
path = {0: [(2, 1), (5, 2), (6, 5), (10, 6)],
1: [(2, 1), (5, 2), (6, 5), (7, 6)],
2: [(2, 5), (3, 2), (5, 6), (6, 10)],
3: [(5, 4), (6, 5), (7, 6)]}
我需要的输出
output_i_need ={0: [(10, 6), (6, 5), (5, 2), (2, 1)],
1: [(7, 6), (6, 5), (5, 2), (2, 1)],
2: [(3, 2), (2, 5), (5, 6), (6, 10)],
3: [(7, 6), (6, 5), (5, 4)]}
python 中是否有图书馆可以订购我的边缘列表?
我不知道有什么库,但是如果将边列表转换为字典映射 edge_start 到 edge_end,那么编写一个函数来重新排序边列表就很容易了。 =11=]
trucks = {0: (10, 1),1:(7,1),2: (3, 10),3:(7,4)} # truck_number:(start_point, end_point)
paths = {0: [(2, 1), (5, 2), (6, 5), (10, 6)],
1: [(2, 1), (5, 2), (6, 5), (7, 6)],
2: [(2, 5), (3, 2), (5, 6), (6, 10)],
3: [(5, 4), (6, 5), (7, 6)]}
def reordered(l, start, end):
d = dict(l)
result = []
while start != end:
result.append((start, d[start]))
start = d[start]
return result
new_paths = { truck: reordered(path, trucks[truck][0], trucks[truck][1])
for truck,path in paths.items() }
print(new_paths)
# {0: [(10, 6), (6, 5), (5, 2), (2, 1)],
# 1: [(7, 6), (6, 5), (5, 2), (2, 1)],
# 2: [(3, 2), (2, 5), (5, 6), (6, 10)],
# 3: [(7, 6), (6, 5), (5, 4)]}
我对每辆卡车的路径有疑问。从我的模型中,我得到每条卡车路径的输出都是错误的。我想根据起点和终点对我的边列表进行排序。
trucks = {0: (10, 1),1:(7,1),2: (3, 10),3:(7,4)} # truck_number:(start_point, end_point)
我需要更改的输入:
path = {0: [(2, 1), (5, 2), (6, 5), (10, 6)],
1: [(2, 1), (5, 2), (6, 5), (7, 6)],
2: [(2, 5), (3, 2), (5, 6), (6, 10)],
3: [(5, 4), (6, 5), (7, 6)]}
我需要的输出
output_i_need ={0: [(10, 6), (6, 5), (5, 2), (2, 1)],
1: [(7, 6), (6, 5), (5, 2), (2, 1)],
2: [(3, 2), (2, 5), (5, 6), (6, 10)],
3: [(7, 6), (6, 5), (5, 4)]}
python 中是否有图书馆可以订购我的边缘列表?
我不知道有什么库,但是如果将边列表转换为字典映射 edge_start 到 edge_end,那么编写一个函数来重新排序边列表就很容易了。 =11=]
trucks = {0: (10, 1),1:(7,1),2: (3, 10),3:(7,4)} # truck_number:(start_point, end_point)
paths = {0: [(2, 1), (5, 2), (6, 5), (10, 6)],
1: [(2, 1), (5, 2), (6, 5), (7, 6)],
2: [(2, 5), (3, 2), (5, 6), (6, 10)],
3: [(5, 4), (6, 5), (7, 6)]}
def reordered(l, start, end):
d = dict(l)
result = []
while start != end:
result.append((start, d[start]))
start = d[start]
return result
new_paths = { truck: reordered(path, trucks[truck][0], trucks[truck][1])
for truck,path in paths.items() }
print(new_paths)
# {0: [(10, 6), (6, 5), (5, 2), (2, 1)],
# 1: [(7, 6), (6, 5), (5, 2), (2, 1)],
# 2: [(3, 2), (2, 5), (5, 6), (6, 10)],
# 3: [(7, 6), (6, 5), (5, 4)]}