当前位置: 动力学知识库 > 问答 > 编程问答 >

python - How to return the index of numpy ndarray based on search?

问题描述:

I have a numpy 2D array,

import numpy as np

array1 = array([[ 1, 2, 1, 1],

[ 2, 2, 2, 1],

[ 1, 1, 1, 1],

[1, 3, 1, 1],

[1, 1, 1, 1]])

I would like to find the element '3' and know its location. So, I could try

condition = array1 == 3

and then to find the arguments, I could try

np.argwhere(condition)

How else does one locate the indices where 3 is located?

网友答案:

You can also use where, returning a tuple of coordinate:

In [34]: np.where(array1==3)
Out[34]: (array([3]), array([1]))
分享给朋友:
您可能感兴趣的文章:
随机阅读: