I would appreciate if somebody could help me with this (and explaining what’s going on).
This works:
>>> from numpy import array
>>> a = array((2, 1))
>>> b = array((3, 3))
>>> l = [a, b]
>>> a in l
True
But this does not:
>>> c = array((2, 1))
>>> c in l
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The behaviour I would like to replicate is:
>>> x = (2, 1)
>>> y = (3, 3)
>>> l2 = [x, y]
>>> z = (2, 1)
>>> z in l2
True
Note that what above also work with mutable objects:
>>> x = [2, 1]
>>> y = [3, 3]
>>> l2 = [x, y]
>>> z = [2, 1]
>>> z in l2
True
Of course, knowing that:
>>> (a < b).all()
True
I tried (and failed):
>>> (c in l).all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Python makes the choice that
bool([False,True])isTruebecause (it says) any non-empy list has boolean value True.Numpy makes the choice that
bool(np.array([False, True]))should raise a ValueError. Numpy was designed from the point of view that some users may want to know if any of the elements in the array are True, while others may want to know if all the elements in the array are True. Since the users may have conflicting desires, NumPy refuses to guess. It raises a ValueError and suggests usingnp.anyornp.all(though if one wishes to replicate Python-like behavior, you’d uselen).When you evaluate
c in l, Python comparescwith each element inlstarting witha. It evaluatesbool(c==a). We getbool(np.array([True True])), which raises a ValueError (for the reason described above).Since numpy refuses to guess, you have to be specific. I suggest: