Reputation: 157
[[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 this list of lists, every number is a location in the route. Now I have to calculate the distance between the edges for every single route.
[(0,100),(100,7),....(127,0)],[(0,2),(2,45) etc.
I already tried the code below but it gives me one list and does not give separate routes.
for a in range(len(result)):
for (i,j) in list(zip(result[a], result[a][1:])):
print((i,j))
Upvotes: 3
Views: 238
Reputation: 17322
if you want to get the tuples of each 2 consecutive numbers from your sublists you may use a list comprehension with the built-in function zip
:
[list(zip(l, l[1:])) for l in result]
output:
[(0,100),(100,7),....(127,0)],[(0,2),(2,45) etc.
Upvotes: 3
Reputation: 54168
For each path (sublist) you may use zip
to pair the elements
edges = []
for path in result:
edges_path = []
for pair in zip(path, path[1:]):
edges_path.append(pair)
edges.append(edges_path)
Using list-comprehension
edges = [list(zip(path, path[1:])) for path in result]
Gives : [[(0, 100), (100, 7), (7, 27), ..., (126, 127), (127, 0)], [(0, 2), (2, 45), ...]
Upvotes: 2