2
In [62]: a
Out[62]: 
array([[1, 2],
       [3, 4]])

Is there an easy way to get [2,3], i.e. the second element of the first row, and the first element of the second row? I have the list of the indices for each row, i.e. [1,0] in this case. I have tried a[:,[1,0]], but it doesn't work.

1 Answer 1

4

You need to specify both i and j for all the elements you want. For example:

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

If you need one item from each row, you can use i = np.arange(a.shape[0])

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.