Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy,其中返回空数组_Python_Arrays_Numpy_Where - Fatal编程技术网

Python Numpy,其中返回空数组

Python Numpy,其中返回空数组,python,arrays,numpy,where,Python,Arrays,Numpy,Where,我有一个数组 a = [5,1,3,0,2] 我应用where函数: np.where(a == 2) 输出为空数组 (array([], dtype=int64),) 我发现了同样的问题,但在我的情况下,它真的没有任何意义或剂量 顺便说一句,我在Mac上使用Python2.7.10您正在将列表传递给where()函数,而不是Numpy数组。改用数组: In [20]: a = np.array([5,1,3,0,2]) In [21]: np.where(a == 2) Out[21]

我有一个数组

a = [5,1,3,0,2]
我应用where函数:

np.where(a == 2)
输出为空数组

(array([], dtype=int64),)
我发现了同样的问题,但在我的情况下,它真的没有任何意义或剂量


顺便说一句,我在Mac上使用Python2.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
,这是传递给
的值,其中
。如果
a
是一个numpy数组,那么
a==2
的值是一个布尔的numpy数组,
where
函数将给出所需的结果。

a
是一个列表,而不是numpy数组。您期望的输出是什么?解释器首先计算
a==2
,然后将结果传递给
where
函数。这个
a==2
有意义吗?实际上,该函数没有应用于列表。问题中
a==2
的值只是
False
,这是传递给
的值,其中
。但是您是正确的,问题在于
a
是一个列表,而不是numpy数组。如果
a
是一个numpy数组,则
a==2
的值是布尔的numpy数组,然后
其中的
按预期工作。@WarrenWeckesser的确如此!谢谢你指出这一点,这可能会导致误解。啊,好吧,现在我明白了!非常感谢您的快速和解释清楚的帮助!