Thank you very much, that works way better!
Now, I would like to do it several times. Let’s say that I have a table of size 5 corresponding to each object’s spatial location. This location varies with time, described by the order_list depending on the step. And to make it simple here we will take:
Step 0: [0,1,2,3,4]
Step 1: [4,0,1,2,3]
Step 2: [3,4,0,1,2]
Step 3: [2,3,4,0,1]
…
Which means that for example, at the step 2 and the location 0, I want to have the value original_list[3], and at the step 3 at the same location I wil have original_list[2], etc.
My problem is that at the beginning the original_list is sorted (as in the example above), but then after the first step, my original list looks like that: [4,0,1,2,3] and after the next switch it should look like [3,4,0,1,2].
I think that an intermediary step is needed, but I can’t find it. Would you have any idea here?
EDIT: here is my code:
import numpy as np
from random import sample
N = 5
original_list = [i for i in range(0,N)]
for K in range(5):
order_list = sample(range(0,N), N) # ORDER
print('Step',K)
print('Original',original_list)
print('Order',order_list)
order_list_map = [np.nan for i in range(N)]
for i in range(N):
order_list_map[order_list[i]] = i
# print(order_list_map)
for i in range(len(order_list_map)):
min_idx = i
for j in range(i+1, len(order_list_map)):
if order_list_map[min_idx] > order_list_map[j]:
min_idx = j
tmp = order_list_map[i]
order_list_map[i] = order_list_map[min_idx]
order_list_map[min_idx] = tmp
tmp = original_list[i]
original_list[i] = original_list[min_idx]
original_list[min_idx] = tmp
print('Final',original_list)
if all([order_list[i]==original_list[i] for i in range(N)]):
print('---> Same order')
else:
print('---> Wrong order')
And the output:
Step 0
Original [0, 1, 2, 3, 4]
Order [2, 0, 1, 3, 4]
Final [2, 0, 1, 3, 4]
---> Same order
Step 1
Original [2, 0, 1, 3, 4]
Order [2, 1, 3, 4, 0]
Final [1, 0, 3, 4, 2]
---> Wrong order
Step 2
Original [1, 0, 3, 4, 2]
Order [0, 3, 2, 4, 1]
Final [1, 4, 3, 2, 0]
---> Wrong order
Step 3
Original [1, 4, 3, 2, 0]
Order [3, 2, 1, 4, 0]
Final [2, 3, 4, 0, 1]
---> Wrong order
Step 4
Original [2, 3, 4, 0, 1]
Order [3, 1, 0, 4, 2]
Final [0, 3, 2, 1, 4]
---> Wrong order