Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.04; 'tree': 0.05; 'nested': 0.07; '[0,': 0.09; 'difference,': 0.09; 'expression:': 0.09; 'subject:test': 0.09; 'cc:addr:python-list': 0.11; '20]': 0.16; '23,': 0.16; 'arithmetic.': 0.16; 'bisect': 0.16; 'breakpoints': 0.16; 'chained': 0.16; 'dict': 0.16; 'expressions,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'readability': 0.16; 'redundancy': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; '15,': 0.26; 'second': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'am,': 0.29; 'ideal': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '>>>>': 0.31; 'this.': 0.32; 'probably': 0.32; 'cases': 0.33; "can't": 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'turn': 0.37; 'list': 0.37; 'easily': 0.37; 'performance': 0.37; 'pm,': 0.38; 'sure': 0.39; 'either': 0.39; 'gone': 0.61; "you're": 0.61; 'making': 0.63; 'kept': 0.65; 'worth': 0.66; 'behavior': 0.77; 'clearer': 0.84; 'results,': 0.84; 'subject::': 0.85; 'canonical': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=FquJcwU83xO1NaY9eKvMEwPKLGndocdawwrTulPmPpg=; b=bF49mNfnLZnA+5JzU49vn66ZDfQt7Ho99TqDpnO4RAMTPpOo3G66eaSbqtlmfwS6I8 jZxj0PpOUyaBTBe1n+4OyWAyVG+V7TZV3BDNFh0WNoIQOKGp+/V3ASaAGQPTUAoqf+qE M4JWbucJ6YkDDDEJ6ML7NxnKkNheS4621NdkLt/qS2xSuvHpkgDaY3HH+PDPLh90zV3S b9f9pR9abMn6sXR3ELf87dTeyt4CT0A9psxi6bFp6U81BMeQkgRZWpPPNESUfWE/yk20 pUzZTWVMm0scR3xfjlboq7IrVGlH3k22SEodwEX6eUMuXIwrWS75a1eRs2hlrsDgQ3kN 7zIA== MIME-Version: 1.0 X-Received: by 10.107.134.75 with SMTP id i72mr20904575iod.53.1414002601679; Wed, 22 Oct 2014 11:30:01 -0700 (PDT) In-Reply-To: <5447F6F4.7080607@case.edu> References: <54476af8$0$21651$426a74cc@news.free.fr> <7839376e-fc27-4299-ae63-4ddf17ef9a8a@googlegroups.com> <5447db41$0$13000$c3e8da3$5496439d@news.astraweb.com> <5447F6F4.7080607@case.edu> Date: Thu, 23 Oct 2014 05:30:01 +1100 Subject: Re: (test) ? a:b From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414002611 news.xs4all.nl 2899 [2001:888:2000:d::a6]:37184 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79466 On Thu, Oct 23, 2014 at 5:27 AM, Matthew Ruffalo wrote: > On 10/22/2014 12:40 PM, Chris Angelico wrote: >> That's true when it's fundamentally arithmetic. But part of that >> readability difference is the redundancy in the second one. What if it >> weren't so redundant? >> >> 'Negative' if x < 0 else 'Low' if x < 10 else 'Mid' if x < 20 else 'High' >> >> You can't easily turn that into a dict lookup, nor indexing. It's >> either a chained if/elif tree or nested if/else expressions, which >> come to the same thing. > No, you can't turn that into a dict lookup, but this is one of the > canonical use cases for the bisect module: > >>>> from bisect import bisect >>>> breakpoints = [0, 10, 20] >>>> labels = ['Negative', 'Low', 'Mid', 'High'] >>>> values = [-5, 5, 15, 25] >>>> [labels[bisect(breakpoints, value)] for value in values] > ['Negative', 'Low', 'Mid', 'High'] > > It's also worth noting that using bisect is O(log(n)) instead of O(n), > but if you're going to hit a point where the asymptotic behavior matters > I'm sure you will have long since abandoned a manually-written if/elif > chain. Indeed. If the performance is making any difference, something's gone wrong. I'm seeing that as not significantly clearer than the chained if/else statement or expression: you need a list of breakpoints and a list of results, which need to be kept in sync. But there's probably no ideal solution to this. ChrisA