gpt4 book ai didi

python - Numpy where 返回空数组

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:59 25 4
gpt4 key购买 nike

我有一个数组,例如

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com