python - Numpy where 返回空数组

标签 python arrays numpy where-clause

我有一个数组,例如

a = [5,1,3,0,2]

我应用 where 函数:

np.where(a == 2)

输出是一个空数组

(array([], dtype=int64),)

我发现了类似的问题 here ,但就我而言,它真的没有任何意义或剂量吗?

顺便说一句。我在 Mac 上使用 Python 2.7.10

最佳答案

您将列表传递给 where() 函数,而不是 Numpy 数组。改为使用数组:

In [20]: a = np.array([5,1,3,0,2])

In [21]: np.where(a == 2)
Out[21]: (array([4]),)

另外如注释中所述,在这种情况下 a == 2 的值是 False,这是传递给 where 的值>。如果 a 是一个 numpy 数组,那么 a == 2 的值是一个 numpy bool 数组,where 函数会给你愿望的结果。

关于python - Numpy where 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48369104/

相关文章:

python - 这个字典理解与这个 "for"循环有什么不同?

python - 检查 df1 中的字符串是否存在于 df2 中的任何位置,并返回 df1 中匹配的列名称

javascript - 只获取一次数据到 React App。页面重新加载后不再获取

python - 为什么会出现int类型错误?类型错误

python - 嵌套 NumPy 数组并使用拆分等方法

python - "(?u)"在正则表达式中做什么?

python - 从只有网站 url 的元标记和 img url 中提取值。为 django 应用程序用 python 编写

python - 使用 matplotlib imshow 在 for 循环中显示 numpy 数组

javascript - 如何检查数组是否为空或存在?

python - numpy append_field 为具有 2d 形状的新字段提供形状错误