mac
mac

Reputation: 43061

Numpy: need a hand in understanding what happens with the "in" operator

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()

Upvotes: 6

Views: 270

Answers (1)

unutbu
unutbu

Reputation: 880587

Python makes the choice that bool([False,True]) is True because (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 using np.any or np.all (though if one wishes to replicate Python-like behavior, you'd use len).

When you evaluate c in l, Python compares c with each element in l starting with a. It evaluates bool(c==a). We get bool(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:

import numpy as np
a=np.array((2,1))
b=np.array((3,3))
c=np.array((2,1))
l=[a,b]
print(any(np.all(c==elt) for elt in l))
# True

Upvotes: 6

Related Questions