3

I have a numpy array vector, and I want to get a subset based on the indexes:

import numpy as np
input=np.array([1,2,3,4,5,6,7,8,9,10])
index=np.array([0,1,0,0,0,0,1,0,0,1])

what is a pythonic way to get out output=[2,7,10]?

1 Answer 1

8
output = input[index.astype(np.bool)]

or

output = input[np.where(index)[0]]
Sign up to request clarification or add additional context in comments.

1 Comment

input[index == 1] might also be worth mentioning.

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.