I have the following list of lists of triples:
all_arcs = [[('Start', 2, 1), (2, 7, 1), (7, 'Stop', 1)],
[('Start', 3, 2), (3, 6, 2), (4, 5, 2), (5, 'Stop', 2), (6, 4, 2)],
[('Start', 1, 3), (1, 8, 3), (8, 9, 3), (9, 'Stop', 3)]]
Every sub-list in all_arcs
is a triplet (i,j,k)
, that denotes the tour i->j
of vehicle k
. So for example, the first sub-list tells us that vehicle 1
has the tour Start -> 2 -> 7 -> Stop
etc.
For every sub-list (<=> for every vehicle) in all_arcs I want to print out the tours in the following format:
Vehicle 1: Start -> 2 -> 7 -> Stop
Vehicle 2: Start -> 3 -> 6 -> 4 -> 5 -> Stop
Vehicle 3: Start -> 1 -> 8 -> 9 -> Stop
...
(Vehicle n: Start -> ... -> ... -> Stop) depending on how many sub-list exists.
So I need to write a function that takes all_arcs
as parameter and prints out the routes in the format above.
My attempt
So I thought It would be a good idea to "chain sort" all the sub-lists first. By chain sort I mean to go from (1,2,0),(3,4,0),(2,3,0)
to (1,2,0),(2,3,0),(3,4,0)
, since the j
index in previous triple is always the i
index in the next. For the sorting, I used these functions and they worked well:
# function that obtains the next arc-tuple given list of tuples
def get_next_tuple(tup ,list_of_tuples):
for i in range(len(list_of_tuples)):
if tup[1] == list_of_tuples[i][0]:
next_tuple = list_of_tuples[i]
return next_tuple
# function that sorts the arc tuples in correct order
def sort_list_of_tuples(list_of_tuples):
sorted_list = [list_of_tuples[0]]
for i in sorted_list:
if len(sorted_list) < len(list_of_tuples):
sorted_list.append(get_next_tuple(i, list_of_tuples))
return sorted_list
Now the printing, this is where I have problems:
for i, arcs in enumerate(all_arcs):
sorted_arcs = sort_list_of_tuples(arcs)
print(f'\nFork-lift {i 1}:')
for j in range(len(sorted_arcs)):
print(f'{sorted_arcs[j][0]} -> {sorted_arcs[j][1]}')
print('\n-----------------------------------')
This obviously works and it prints out the correct order:
Vehicle 1:
Start -> 2
2 -> 7
7 -> Stop
-----------------------------------
Vehicle 2:
Start -> 3
3 -> 6
6 -> 4
4 -> 5
5 -> Stop
-----------------------------------
Vehicle 3:
Start -> 1
1 -> 8
8 -> 9
9 -> Stop
-----------------------------------
However, I either want it to print like the format above or(and) simply store the routes in simple lists, like this:
Vehicle 1 = [Start, 2, 7, Stop]
Vehicle 2 = [Start, 3, 6, 4, 5, Stop]
Vehicle 3 = [Start, 1, 8, 9, Stop]
The code is completely reproducible. I appreciate any help.
CodePudding user response:
I would use dict:
all_arcs = [[('Start', 2, 1), (2, 7, 1), (7, 'Stop', 1)],
[('Start', 3, 2), (3, 6, 2), (4, 5, 2), (5, 'Stop', 2), (6, 4, 2)],
[('Start', 1, 3), (1, 8, 3), (8, 9, 3), (9, 'Stop', 3)]]
for arc in all_arcs:
vehicle = arc[0][2]
dct = {a: b for a, b, _ in arc} # a mapping of a point to another point
current = 'Start'
path = [current]
while current != 'Stop':
path.append(current := dct[current])
print(f"Vehicle {vehicle}:", ' -> '.join(map(str, path)))
# Vehicle 1: Start -> 2 -> 7 -> Stop
# Vehicle 2: Start -> 3 -> 6 -> 4 -> 5 -> Stop
# Vehicle 3: Start -> 1 -> 8 -> 9 -> Stop
For each sublist (arc
in the code), I construct a dictionary dct
that maps a point to another point. Then chaining the paths according to this mapping is easier afterwards.