0
import numpy as np
a = np.array([4, 3, 2, 1])
b = np.array([1, 2, 4, 3])
c = np.stack((a, b))
i = np.array([0, 1])
print(c[i])

I got:

[[4 3 2 1]
 [1 2 4 3]]

but my expectd output:

[4 2]

How can I implement this?

3
  • try print(c[[0,1], [0,1]]) Commented Oct 12, 2020 at 10:08
  • 1
    c[np.arange(c.shape[0]), i] Commented Oct 12, 2020 at 10:08
  • 1
    @hilberts_drinking_problem that's exactly what I want, thanks a lot. Commented Oct 12, 2020 at 10:27

1 Answer 1

1

What do you want to achieve here?

c = [[4 3 2 1] [1 2 4 3]]; from np.stack call

[c[0][0],c[1][1]] returns [4 2] which is what you want.

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.