1

I've tried np.take but I don't think that's what I'm looking for. Here's what I want to do:

arr = np.array([[3, 4], [5, 6], [7, 8]])
indexes = np.array([0, 1, 0])

n = len(arr)
result = np.zeros(n)
for i in range(n):
    result[i] = arr[i, indexes[i]]

Sorry if I just don't understand how to use np.take and that's what it is for.

2
  • 1
    result = arr[np.arange(n), indexes]? Commented Nov 12, 2020 at 1:52
  • Yes, it looks like that will work. Thanks. Commented Nov 12, 2020 at 1:58

1 Answer 1

1

Advanced indexing will also keep the dtype:

arr[np.arange(len(arr)), indexes]
#[3 6 7]
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.