numpy.argwhere#

numpy.argwhere(a)[源码]#

查找数组中非零元素的索引,按元素分组。

参数:
a类数组

输入数据。

返回:
index_array(N, a.ndim) ndarray

非零元素的索引。索引按元素分组。此数组的形状为 (N, a.ndim),其中 N 是非零项的数量。

另请参阅

where, nonzero

注意

np.argwhere(a)np.transpose(np.nonzero(a)) 几乎相同,但对于 0D 数组会生成正确形状的结果。

argwhere 的输出不适用于数组索引。为此,请改用 nonzero(a)

示例

>>> import numpy as np
>>> x = np.arange(6).reshape(2,3)
>>> x
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.argwhere(x>1)
array([[0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])