[英]Numpy bug? (python3)
import numpy as np
mainList = []
numpyArray0 = np.array([1,2,3])
numpyArray1 = np.array([4,5,6])
mainList.append(numpyArray0)
mainList.append(numpyArray1)
print("numpyArray0 in mainList:")
try:
print(numpyArray0 in mainList)
except ValueError:
print("ValueError")
print("numpyArray1 in mainList:")
try:
print(numpyArray1 in mainList)
except ValueError:
print("ValueError")
print("mainList in numpyArray0:")
try:
print(mainList in numpyArray0)
except ValueError:
print("ValueError")
print("mainList in numpyArray1:")
try:
print(mainList in numpyArray1)
except ValueError:
print("ValueError")
print(numpyArray1 in mainList)
So I have the above code basically it creates 2 numpy arrays inside a normal python list (mainList) and then it checks to see if those 2 arrays are inside the list. 所以我有上面的代码基本上它在普通的python列表(mainList)中创建2个numpy数组,然后它检查这两个数组是否在列表中。 The code should output:
代码应该输出:
numpyArray0 in mainList:
True
numpyArray1 in mainList:
**True**
mainList in numpyArray0:
True
mainList in numpyArray1:
True
**True**
But instead of outputing the above it outputs the following: 但不输出上述内容,而是输出以下内容:
numpyArray0 in mainList:
True
numpyArray1 in mainList:
ValueError
mainList in numpyArray0:
True
mainList in numpyArray1:
True
Traceback (most recent call last):
File "/home/user/Documents/pythonCode/temp.py", line 31, in <module>
print(numpyArray1 in mainList)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Am I doing anything incorrectly? 我做错了吗? Note that I tried updating python, numpy, and my os (debian) before running the code.
请注意,我在运行代码之前尝试更新python,numpy和我的os(debian)。
numpyArray0 in mainList
calls list.__contains__
. numpyArray0 in mainList
调用list.__contains__
numpyArray0 in mainList
list.__contains__
。 A list's __contains__
method calls PyObject_RichCompareBool
for each element of the list to check if the elements are equal. 列表的
__contains__
方法为列表的每个元素调用PyObject_RichCompareBool
以检查元素是否相等。 As it happens, PyObject_RichCompareBool
checks for identity equality first, and then does a full comparison. 碰巧,
PyObject_RichCompareBool
检查身份相等,然后进行完全比较。
numpyArray0 is mainList[0]
returns True
, so full comparison is never done. numpyArray0 is mainList[0]
返回True
,因此永远不会进行完全比较。 If full comparison was done, numpy
would raise ValueError
since a numpy
array cannot be interepreted as a boolean. 如果完全比较,
numpy
会引发ValueError
因为numpy
数组不能作为布尔值解释。
numpyArray1 in mainList
shows that as well (since identity comparison fails for numpyArray1
vs mainList[0]
. numpyArray1 in mainList
显示(因为numpyArray1
与mainList[0]
身份比较失败mainList[0]
。
看起来它是与Numpy数组的==
运算符重载方式相关的众所周知的功能 。
here the gist: 这里的要点:
>>> numpyArray1 in mainList
....
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
this will work: 这将工作:
>>> any([list(numpyArray1) == list(litem) for litem in mainList])
True
Compare the same operations with lists instead of numpy arrays : 将相同的操作与列表而不是numpy数组进行比较 :
In[171]: mainList = []
In[172]: list0 = [1,2,3]
In[173]: list1 = [4,5,6]
In[174]: mainList.append(list0)
In[175]: mainList.append(list1)
In[176]: list0 in mainList
Out[176]: True
In[177]: list1 in mainList
Out[177]: True
What am I trying to show with this? 我想用这个来展示什么?
Two things. 两件事情。
cache = [] cache.append(numpy.ndarray([1,2,3])) cache.append(numpy.ndarray([4,5,6])) numpy.ndarray([4,5,6]) in cache
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.