Python: Order two arrays according to third array

Viewed 69

I have three arrays:

a = np.arange(10)
np.shuffle(a)
b = np.random.permutation(a)
c = b+10

c is in biunivocal correspondence with b, which is a mixed version of a. I would like to put the elements of c in the same order of those in a. For example:

a = [0 2 4 3 1 5 6 7 8 9]
b = [0 3 9 1 8 6 4 7 2 5]
c = [10 13 19 11 18 16 14 17 12 15]

I would like:

b = [0 2 4 3 1 5 6 7 8 9]
c = [10 12 14 13 11 15 16 17 18 19]

I want to reorder c according to a

1 Answers

A soution with lists:

a_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b_list = [0, 3, 9, 1, 8, 6, 4, 7, 2, 5]
c_list = [10, 13, 19, 11, 18, 16, 14, 17, 12, 15]

decorated_c = [(a_list.index(b),c) for (b,c) in zip(b_list,c_list)]
_, result = zip(*sorted(decorated_c))
print(result)

The output is:

(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)

It should work even if a_list is not sorted

Related