Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'friday,': 0.09; 'nice!': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'assume': 0.14; 'boolean': 0.16; 'evaluated.': 0.16; 'expressions.': 0.16; 'fail,': 0.16; 'implies': 0.16; 'numpy': 0.16; 'overridden': 0.16; 'overriding': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'unlike': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'options': 0.25; 'equivalent': 0.26; 'header:X-Complaints-To:1': 0.27; 'comparison': 0.31; 'thanks!': 0.32; 'guess': 0.33; 'common': 0.35; 'january': 0.37; 'wrong': 0.37; 'so,': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'john': 0.61; 'happen': 0.63; 'skip:n 10': 0.64; 'smith': 0.68; '(*),': 0.84; 'yours.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: numpy.where() and multiple comparisons Date: Sat, 18 Jan 2014 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> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a8d5.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390034994 news.xs4all.nl 2852 [2001:888:2000:d::a6]:59156 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64220 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.