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


Groups > comp.lang.python > #79465

Re: (test) ? a:b

Return-Path <mmr15@case.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'skip:[ 20': 0.04; 'tree': 0.05; 'nested': 0.07; '[0,': 0.09; 'subject:test': 0.09; 'cc:addr:python-list': 0.11; '20]': 0.16; 'arithmetic.': 0.16; 'bisect': 0.16; 'breakpoints': 0.16; 'chained': 0.16; 'dict': 0.16; 'expressions,': 0.16; 'readability': 0.16; 'received:209.85.223.173': 0.16; 'redundancy': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; '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; "i'm": 0.30; 'cases': 0.33; "can't": 0.35; 'received:209.85': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'turn': 0.37; 'received:209': 0.37; 'easily': 0.37; 'pm,': 0.38; 'sure': 0.39; 'either': 0.39; "you're": 0.61; 'to:addr:gmail.com': 0.65; 'worth': 0.66; 'behavior': 0.77; 'subject::': 0.85; 'canonical': 0.91
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=E+Xz3IYIg+7+k3XCR2gpF/G8+zrKZTVIpf/MrLQNEtM=; b=AxvzOjN52oSNfS7DPuQp4hLMRNWGo0vKOKUWOFic2dH+ejoJhhkI2jRoeq/RKp8bhA qejpYnrchz2wJItSskgA0qD1eqDuikrR4SmDEROwWZog9E2qok1Xov7Y97FnweV7rEjN GdYtbgtWlxJxtiSXkhJoVRar6fqokdGaaRPbJpv804/X+7/jVS95apX8o7Z6+g6UBwLI yWXFU+8qKhvguEKY2T3SOp2QfqrBoBb/U3nKmRozipIG2vvf9sKW2YC7276yw3mqDPBl xLvP7oI7QNOG+uySpS+4Hi5mBkGSLOdAAvUN+RVLz2avHCnMT1l/zX3tFubQf/+QEvzw SLEg==
X-Gm-Message-State ALoCoQnKOLmIKzAuEa2+DmXZdnQCB+nZpPClUw0MkrfTf1t1QPyBFy6M57GiOFNeWLf93o6T0NmNPXDxPiWjiKCJIdiWwL8y11MbFTakoa09oqUgTFVdevwvbRK3KtHt9XWuSqPNrpDB
X-Received by 10.107.3.25 with SMTP id 25mr3719780iod.69.1414002425508; Wed, 22 Oct 2014 11:27:05 -0700 (PDT)
X-Received by 10.107.3.25 with SMTP id 25mr3719431iod.69.1414002421785; Wed, 22 Oct 2014 11:27:01 -0700 (PDT)
From Matthew Ruffalo <mmr15@case.edu>
X-Google-Original-From Matthew Ruffalo <matthew.ruffalo@case.edu>
Date Wed, 22 Oct 2014 14:27:00 -0400
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
MIME-Version 1.0
To Chris Angelico <rosuav@gmail.com>
Subject Re: (test) ? a:b
References <54476af8$0$21651$426a74cc@news.free.fr> <7839376e-fc27-4299-ae63-4ddf17ef9a8a@googlegroups.com> <mailman.15068.1413977455.18130.python-list@python.org> <5447db41$0$13000$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmpVk34kpL1kY=7rPiD5OpctdLsttBb5=1VyeMCQyhJ2CQ@mail.gmail.com>
In-Reply-To <CAPTjJmpVk34kpL1kY=7rPiD5OpctdLsttBb5=1VyeMCQyhJ2CQ@mail.gmail.com>
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding quoted-printable
X-Junkmail-Whitelist YES (by domain whitelist at mpv1.tis.cwru.edu)
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15088.1414002429.18130.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414002429 news.xs4all.nl 2935 [2001:888:2000:d::a6]:34524
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:79465

Show key headers only | View raw


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.

MMR...

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


Thread

(test) ? a:b "ast" <nomail@invalid.com> - 2014-10-22 10:29 +0200
  Re: (test) ? a:b buscacio@gmail.com - 2014-10-22 02:05 -0700
    Re: (test) ? a:b "ast" <nomail@invalid.com> - 2014-10-22 11:14 +0200
      Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-22 12:45 +0100
        Re: (test) ? a:b "ast" <nomail@invalid.com> - 2014-10-27 09:01 +0100
      Re: (test) ? a:b Michael Torrie <torriem@gmail.com> - 2014-10-22 11:16 -0600
        Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-23 11:44 +1100
    Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-22 20:15 +1100
    Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-22 10:16 +0100
    Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-22 20:27 +1100
      Re: (test) ? a:b Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-23 16:46 +1300
        Re: (test) ? a:b MRAB <python@mrabarnett.plus.com> - 2014-10-23 11:12 +0100
          Re: (test) ? a:b "ast" <nomail@invalid.com> - 2014-10-24 08:38 +0200
        Re: (test) ? a:b Michael Torrie <torriem@gmail.com> - 2014-10-25 19:04 -0600
          Re: (test) ? a:b Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-27 22:04 +1300
        Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 12:20 +1100
        Re: (test) ? a:b Michael Torrie <torriem@gmail.com> - 2014-10-25 21:05 -0600
    Re: (test) ? a:b Ned Batchelder <ned@nedbatchelder.com> - 2014-10-22 07:30 -0400
      Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-22 14:41 +0300
      Re: (test) ? a:b Rustom Mody <rustompmody@gmail.com> - 2014-10-22 06:16 -0700
        Re: (test) ? a:b "BartC" <bc@freeuk.com> - 2014-10-22 19:08 +0100
          Re: (test) ? a:b Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-22 19:55 -0400
            Re: (test) ? a:b "BartC" <bc@freeuk.com> - 2014-10-23 11:50 +0100
              Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-23 16:44 +0300
                Re: (test) ? a:b Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-23 08:24 -0600
                Re: (test) ? a:b Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-10-23 16:41 +0200
                Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-23 19:20 +0300
                Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-24 19:38 +1100
                Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-24 19:45 +1100
                Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-24 12:37 +0300
                Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-25 00:07 +1100
                Re: (test) ? a:b Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-24 12:22 -0600
                Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-24 21:46 +0300
                Re: (test) ? a:b Dan Stromberg <drsalists@gmail.com> - 2014-10-24 10:20 -0700
                Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-24 20:58 +0000
                Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-25 00:02 +0300
                Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-25 16:03 +1100
                Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-25 16:25 +1100
                Re: (test) ? a:b Ned Batchelder <ned@nedbatchelder.com> - 2014-10-25 07:58 -0400
                Re: (test) ? a:b Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-25 07:53 -0600
                Re: (test) ? a:b Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-25 14:11 -0400
                Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-25 19:49 +0000
                Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 07:45 +1100
                Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-26 11:10 +1100
                Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 12:03 +1100
                Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-26 15:15 +1100
                Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 15:34 +1100
                Re: (test) ? a:b Terry Reedy <tjreedy@udel.edu> - 2014-10-26 17:12 -0400
                Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-27 11:38 +1100
                Re: (test) ? a:b MRAB <python@mrabarnett.plus.com> - 2014-10-27 01:40 +0000
                Re: (test) ? a:b Rustom Mody <rustompmody@gmail.com> - 2014-10-26 19:19 -0700
                Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 12:10 +1100
                Re: (test) ? a:b Joshua Landau <joshua@landau.ws> - 2014-10-27 01:55 +0000
                Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-27 13:28 +1100
                Re: (test) ? a:b Rustom Mody <rustompmody@gmail.com> - 2014-10-26 19:38 -0700
                Re: (test) ? a:b Joshua Landau <joshua@landau.ws> - 2014-10-27 02:43 +0000
                Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-27 13:56 +1100
                Re: (test) ? a:b Dan Stromberg <drsalists@gmail.com> - 2014-10-25 15:34 -0700
                Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 10:53 +1100
                Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 11:01 +1100
                Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-26 01:09 +0100
                Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 11:13 +1100
          Re: (test) ? a:b Vito De Tullio <vito.detullio@gmail.com> - 2014-10-23 06:55 +0200
      Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-23 03:28 +1100
        Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-23 03:40 +1100
        Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-22 18:03 +0000
        Re: (test) ? a:b Matthew Ruffalo <mmr15@case.edu> - 2014-10-22 14:27 -0400
        Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-23 05:30 +1100
        Re: (test) ? a:b Ned Batchelder <ned@nedbatchelder.com> - 2014-10-22 16:11 -0400
    Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-22 12:41 +0100
      Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-22 15:12 +0000
        Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-23 02:18 +1100
          Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-22 15:24 +0000
    Re: (test) ? a:b Dan Stromberg <drsalists@gmail.com> - 2014-10-22 09:09 -0700
  Re: (test) ? a:b Tobiah <toby@tobiah.org> - 2014-10-24 10:23 -0700
    Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-25 04:27 +1100
      Re: (test) ? a:b Tobiah <toby@tobiah.org> - 2014-10-24 11:31 -0700

csiph-web