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]])