numpy.argwhere#

numpy.argwhere(a)[原始碼]#

尋找陣列元素中非零元素的索引,依元素分組。

參數:
aarray_like

輸入資料。

回傳值:
index_array(N, a.ndim) ndarray

非零元素的索引。索引依元素分組。此陣列的形狀為 (N, a.ndim),其中 N 是非零項目的數量。

參見

wherenonzero

註解

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