2

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

5
  • what rules would you apply? is it even possible to get state you want? Show me an example where You are ordering after unordered list. By hand of course, Commented May 8, 2021 at 12:23
  • The idea was to use b to sort c, since b is an unordered version of a. But I don't know how to do that Commented May 8, 2021 at 12:26
  • 1
    "I would like to put the elements of c in the same order of those in a". a is already sorted so this is effectively doing nothing? Commented May 8, 2021 at 12:31
  • a is not necessarily sorted in an intuitive way, I made it simple in the example Commented May 8, 2021 at 12:38
  • why not just add +10 to a ? Commented May 8, 2021 at 13:18

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.