[Numpy-discussion] multi-comparison expressions
John
john@saponara....
Mon Oct 6 14:32:55 CDT 2008
hi,
why does the ValueError appear below, and how can i make that 2<a<5
expression work when a is an array?
thanks.
>>> from numpy import reshape,arange
>>> a=reshape(arange(9),(3,3))
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> 2<a
array([[False, False, False],
[ True, True, True],
[ True, True, True]], dtype=bool)
>>> a<5
array([[ True, True, True],
[ True, True, False],
[False, False, False]], dtype=bool)
>>> 2<a<5
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()
nor does it work with constant arrays:
>>> from numpy import zeros
>>> twos=zeros(a.shape)+2
>>> fives=zeros(a.shape)+5
>>> twos<a
array([[False, False, False],
[ True, True, True],
[ True, True, True]], dtype=bool)
>>> a<fives
array([[ True, True, True],
[ True, True, False],
[False, False, False]], dtype=bool)
>>> twos<a<fives
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()
but it works with builtin numbers:
>>> b=3
>>> 2<b<5
True
More information about the Numpy-discussion
mailing list