Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64220

Re: numpy.where() and multiple comparisons

From Peter Otten <__peter__@web.de>
Subject Re: numpy.where() and multiple comparisons
Date 2014-01-18 09:50 +0100
Organization None
References <5617a90f-3d9b-48b2-b449-9e5ef4c181e5@googlegroups.com> <52d9e408$0$29769$862e30e2@ngroups.net> <39d6bb6a-ce34-469e-8cb3-24a14331d6c5@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5673.1390034994.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

numpy.where() and multiple comparisons John Ladasky <john_ladasky@sbcglobal.net> - 2014-01-17 17:51 -0800
  Re: numpy.where() and multiple comparisons duncan smith <buzzard@invalid.invalid> - 2014-01-18 02:16 +0000
    Re: numpy.where() and multiple comparisons John Ladasky <john_ladasky@sbcglobal.net> - 2014-01-17 20:00 -0800
      Re: numpy.where() and multiple comparisons Peter Otten <__peter__@web.de> - 2014-01-18 09:50 +0100
        Re: numpy.where() and multiple comparisons Tim Roberts <timr@probo.com> - 2014-01-18 13:20 -0800
      'and' is not exactly an 'operator' (was Re: numpy.where() and multiple comparisons) Terry Reedy <tjreedy@udel.edu> - 2014-01-18 19:12 -0500

csiph-web