numpy.
extract
返回满足某些条件的数组元素。
这相当于 np.compress(ravel(condition), ravel(arr)) . 如果 condition 是布尔型 np.extract 等于 arr[condition] .
np.compress(ravel(condition), ravel(arr))
np.extract
arr[condition]
注意 place 正好相反吗 extract .
place
一种数组,其非零或真项指示 arr 提取。
与相同大小的输入数组 condition .
排名1的值数组来自 arr 在哪里? condition 是True。
参见
take
put
copyto
compress
实例
>>> 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])