0

Suppose I have an index array:

A = [1, 0, 3, 2, 0, 1]

for another array of values:

B = [21, 33, 10, 2]

Then I want a new array:

C = [B[A[0]], B[[A[1]], ..., B[A[[5]]] = [B[1], B[0], ..., B[1]] = [33, 21, ..., 21]

How does one do this with numpy arrays?

1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Nov 28, 2021 at 11:32

1 Answer 1

1

You can apply numpy indexing when B is a numpy array:

import numpy as np

A = [1, 0, 3, 2, 0, 1]
B = np.array([21, 33, 10, 2])
output = B[A]
print(output) # [33 21  2 10 21 33]
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.