Numpy 'where' 为条件 >= 0 的所有 0s 数组返回空数组



我使用np。Where函数用于获取值与0匹配的索引。但是下面的代码返回空数组,这是不期望的。

import numpy as np
a = np.array([0,0,0,0])
np.where(a[a>=0])

得到:(array([], dtype=int64),).

谁能指出我错过了什么?

https://numpy.org/doc/stable/reference/generated/numpy.where.html

这是意料之中的。a>=0给你所有的True,即a[a>=0]给你a,它不包含任何非零元素。所以np.where(a)返回空数组

您在找np.where(a>=0)吗?

你是在告诉你找到a[a>=0]的索引a>=0返回[True,True,True,True],则a[a>=0]返回[0,0,0,0],则没有True条件可返回。

也许你想做

np.where(a>=0)