numpy.extract#

numpy.extract(condition, arr)[原始碼]#

傳回陣列中滿足某些條件的元素。

這等效於 np.compress(ravel(condition), ravel(arr))。如果 condition 是布林值,則 np.extract 等效於 arr[condition]

請注意,place 的作用與 extract 完全相反。

參數:
conditionarray_like

一個陣列,其非零或 True 的條目指示要提取的 arr 元素。

arrarray_like

condition 大小相同的輸入陣列。

傳回值:
extractndarray

來自 arr 的 Rank 1 數值陣列,其中 condition 為 True。

另請參閱

take, put, copyto, compress, place

範例

>>> import numpy as np
>>> arr = np.arange(12).reshape((3, 4))
>>> arr
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> condition = np.mod(arr, 3)==0
>>> condition
array([[ True, False, False,  True],
       [False, False,  True, False],
       [False,  True, False, False]])
>>> np.extract(condition, arr)
array([0, 3, 6, 9])

如果 condition 是布林值

>>> arr[condition]
array([0, 3, 6, 9])