numpy.where() and multiple comparisons
Peter Otten
__peter__ at web.de
Sat Jan 18 03:50:00 EST 2014
John Ladasky wrote:
> On Friday, January 17, 2014 6:16:28 PM UTC-8, duncan smith wrote:
>
>> >>> a = np.arange(10)
>> >>> c = np.where((2 < a) & (a < 7))
>> >>> c
>> (array([3, 4, 5, 6]),)
>
> Nice! Thanks!
>
> Now, why does the multiple comparison fail, if you happen to know?
2 < a < 7
is equivalent to
2 < a and a < 7
Unlike `&` `and` cannot be overridden (*), so the above implies that the
boolean value bool(2 < a) is evaluated. That triggers the error because the
numpy authors refused to guess -- and rightly so, as both implementable
options would be wrong in a common case like yours.
(*) I assume overriding would collide with short-cutting of boolean
expressions.
More information about the Python-list
mailing list