Search code examples
pythonlistdictionaryedges

Find distance for every edge and keep separate routes


[[0, 100, 7, 27, 34, 40, 41, 48, 58, 65, 75, 78, 79, 96, 126, 127, 0],
[0, 2, 45, 54, 56, 57, 59, 66, 67, 82, 86, 102, 124, 133, 0],
[0, 35, 39, 52, 53, 60, 61, 80, 81, 83, 87, 97, 98, 101, 109, 0],
[0, 15, 28, 29, 30, 31, 32, 33, 37, 38, 49, 50, 51, 71, 95, 0],
[0, 3, 16, 22, 23, 44, 72, 73, 74, 90, 110, 131, 0],
[0, 10, 11, 18, 19, 36, 55, 89, 93, 94, 108, 113, 114, 0],
[0, 1, 5, 6, 9, 12, 17, 24, 43, 64, 77, 85, 88, 91, 92, 111, 112, 130, 0],
[0, 13, 20, 42, 62, 68, 84, 99, 104, 116, 119, 125, 128, 129, 132, 0],
[0, 8, 14, 26, 63, 69, 70, 103, 105, 123, 0],
[0, 4, 21, 25, 46, 47, 106, 107, 115, 117, 118, 120, 121, 122, 0],
[0, 76, 0]]

I have the different routes listed above. I need to calculate the distance of every route (11 routes in total)

After this, I have created all edges within a single route.

[[(0, 100), (100, 7), (7, 27), (27, 34), (34, 40), (40, 41), (41, 48), (48, 58), (58, 65), (65, 75), (75, 78), (78, 79), (79, 96), (96, 126), (126, 127), (127, 0)], [(0, 2), (2, 45), (45, 54), (54, 56), (56, 57), (57, 59), (59, 66), (66, 67), (67, 82), (82, 86), (86, 102), (102, 124), (124, 133), (133, 0)], [(0, 35), (35, 39), (39, 52), (52, 53), (53, 60), (60, 61), (61, 80), (80, 81), (81, 83), (83, 87), (87, 97), (97, 98), (98, 101), (101, 109), (109, 0)], [(0, 15), (15, 28), (28, 29), (29, 30), (30, 31), (31, 32), (32, 33), (33, 37), (37, 38), (38, 49), (49, 50), (50, 51), (51, 71), (71, 95), (95, 0)], [(0, 3), (3, 16), (16, 22), (22, 23), (23, 44), (44, 72), (72, 73), (73, 74), (74, 90), (90, 110), (110, 131), (131, 0)], [(0, 10), (10, 11), (11, 18), (18, 19), (19, 36), (36, 55), (55, 89), (89, 93), (93, 94), (94, 108), (108, 113), (113, 114), (114, 0)], [(0, 1), (1, 5), (5, 6), (6, 9), (9, 12), (12, 17), (17, 24), (24, 43), (43, 64), (64, 77), (77, 85), (85, 88), (88, 91), (91, 92), (92, 111), (111, 112), (112, 130), (130, 0)], [(0, 13), (13, 20), (20, 42), (42, 62), (62, 68), (68, 84), (84, 99), (99, 104), (104, 116), (116, 119), (119, 125), (125, 128), (128, 129), (129, 132), (132, 0)], [(0, 8), (8, 14), (14, 26), (26, 63), (63, 69), (69, 70), (70, 103), (103, 105), (105, 123), (123, 0)], [(0, 4), (4, 21), (21, 25), (25, 46), (46, 47), (47, 106), (106, 107), (107, 115), (115, 117), (117, 118), (118, 120), (120, 121), (121, 122), (122, 0)], [(0, 76), (76, 0)]]

However, I need to calculate the distance between the edges. Every edge consists of 2 numbers which are city numbers in a distance matrix (so 0,100 is the distance from city 0 to city 100). I tried to calculate the distances but cannot keep separate routes.

I already tried this:

a_list=[]
visiting_time={}
for k in range(len(result)):
    for (i,j) in visits[k]:
        visiting_time[(i,j)]= distance_matrix_new_time[i][j]
        f=list(visiting_time.values())
    a_list.append(f)

In my code Result is the list with different routes (first list), and visits is the list with all edges (second list)

the output should be like this

[2,3,5,6,3,2,5,8,3,5,2,4,6],[2,6,3,1,9,....],[....] etc.

Can someone help me out?


Solution

  • you could use a list comprehension:

    a_list = [[distance_matrix_new_time[i][j] for i, j in l] for l in  visits]