Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95122
| Path | csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ahlusar.ahluwalia@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'squares': 0.07; 'subject:skip:c 10': 0.07; 'cc:addr:python-list': 0.09; 'behave': 0.09; 'example:': 0.10; 'do,': 0.15; 'wed,': 0.15; 'blame': 0.16; 'clauses': 0.16; 'comp': 0.16; 'from:addr:ahlusar.ahluwalia': 0.16; 'from:name:saran ahluwalia': 0.16; 'range(20):': 0.16; 'something.': 0.16; 'unwound': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'nested': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'do.': 0.22; 'function,': 0.22; 'minor': 0.22; 'suppose': 0.22; 'am,': 0.23; 'code,': 0.23; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'rest': 0.26; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; 'correct,': 0.29; 'that.': 0.30; 'too.': 0.30; 'url:mailman': 0.30; 'code': 0.30; 'call.': 0.30; 'certainly': 0.30; "i'd": 0.31; 'rules': 0.31; '[1]': 0.32; 'statement': 0.32; 'url:python': 0.33; 'call,': 0.33; 'combination': 0.33; 'version:': 0.33; 'url:listinfo': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'skip:( 30': 0.35; 'could': 0.35; 'done': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'beginning': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'method': 0.37; "won't": 0.38; 'skip:o 20': 0.38; 'sure': 0.39; 'url:mail': 0.40; 'subject:with': 0.40; 'some': 0.40; 'matter': 0.63; 'iphone': 0.63; 'skip:w 30': 0.64; 'friend,': 0.70; "'for'": 0.84; 'chrisa': 0.84; 'expect.': 0.84; 'filtered': 0.84; 'technically': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=gbwVBWLZtxivgHyjj/NDOHRX8SNWlS19+xnu9uOghac=; b=oAg323tvv+AA4ZkIY7IWInNeISD7lDrldBxaakedGOB+t9FmDKMlPYnf+CKFIYQCSR LxDr6txJ8IFRYXb3k5XjTuhO9MI+FyWGr7naY8GNTL+Jv6EdyvNTeA7+snNqOYNc1zs3 IzQGn3Ub0ZPfYM6fVqX6LpI2i21/NUp46eBmYTg5W+pzLDdJQl+malfCr7mTur5p8B9a DAySkLHFbg8c2Nl6FizFt43YVLXHrkpvJx2N7vP6O8lZSEzzpPAcbNmeosQneBUKtGMj 15xM8EWExZ+z7oXoJAK1hGSRGcUi6KSMEPeW1C1gVx21G2E1kJlGShxY1OmmwPLrpQ7Z SyAQ== |
| X-Received | by 10.180.74.229 with SMTP id x5mr6743290wiv.90.1438952904457; Fri, 07 Aug 2015 06:08:24 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <CAPTjJmrAp1sV-YNZ3NOpfT19E627sQ=cX+_UxbYOkQ1jVQHqtw@mail.gmail.com> |
| References | <ebf0a31e-1021-41d2-affb-8318838a464b@googlegroups.com> <mailman.1231.1438758358.3674.python-list@python.org> <87wpxacdnr.fsf@elektro.pacujo.net> <CAPTjJmrAp1sV-YNZ3NOpfT19E627sQ=cX+_UxbYOkQ1jVQHqtw@mail.gmail.com> |
| From | Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> |
| Date | Fri, 7 Aug 2015 09:08:05 -0400 |
| Subject | Re: GOTCHA with list comprehension |
| To | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| 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.1305.1438952908.3674.python-list@python.org> (permalink) |
| Lines | 85 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1438952908 news.xs4all.nl 2935 [2001:888:2000:d::a6]:43571 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:95122 |
Show key headers only | View raw
@ChrisA You, my friend, certainly put the nail in the coffin! Sent from my iPhone > On Aug 5, 2015, at 5:52 AM, Chris Angelico <rosuav@gmail.com> wrote: > >> On Wed, Aug 5, 2015 at 7:01 PM, Marko Rauhamaa <marko@pacujo.net> wrote: >> Chris Angelico <rosuav@gmail.com>: >> >>> You can chain 'for' and 'if' clauses as much as you like, and they >>> behave exactly the way you'd expect. >> >> How do you know what I'd expect? >> >> I wouldn't know what to expect myself. > > A list comprehension can always be unwound into statement form. [1] For example: > > squares = [n*n for n in range(10)] > > can be unwound into: > > squares = [] > for n in range(10): > squares.append(n*n) > > You simply take the expression at the beginning and put that into the > append() method call, and then the rest become statements. Here's a > filtered version: > > odd_squares = [n*n for n in range(20) if n%2] > > Which becomes: > > odd_squares = [] > for n in range(20): > if n%2: > odd_squares.append(n*n) > > So what would you expect nested 'if' clauses to do? Well, they become > nested 'if' statements: > > primes = [n for n in range(2,24) if n%2 if n%3] > > primes = [] > for n in range(2,24): > if n%2: > if n%3: > primes.append(n) > > What if we have multiple 'for' loops? Same thing! > > ways_to_get_seven = [(a,b) for a in range(1,7) for b in range(1,7) if a+b==7] > > ways_to_get_seven = [] > for a in range(1,7): > for b in range(1,7): > if a+b==7: > ways_to_get_seven.append((a,b)) > > No matter what combination of 'if' and 'for' you use, it can be > unwound like this, and it'll always behave the same way. Not sure what > to expect? Follow the simple rules of unwinding, and then read the > code that way. > > Of course, if you don't know how to predict what the statement form > will do, then you won't understand what the comprehension will do. But > that's not the comprehension's fault. > > peculiar = [a*x*x+b*x+c for a in range(1,10) for b in range(2,30) for > c in range(-3,5) if b*b-4*a*c>=0 if (-b+math.sqrt(b*b-4*a*c))/2/a>0 > for x in (x*.01 for x in range(-500,500))] > > I suppose you could graph that. Or something. But that's just bad > code, don't blame the list comp for that... > > ChrisA > > [1] To be technically correct, this unwinding should be done in a > nested function, which you then call. There are some other minor > technicalities too. But it's pretty much this. > -- > https://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-04 23:48 -0700
Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:03 -0700
Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:20 +1000
Re: GOTCHA with list comprehension Peter Otten <__peter__@web.de> - 2015-08-05 09:04 +0200
Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:05 +1000
Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:10 -0700
Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:21 +1000
Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:33 -0700
Re: GOTCHA with list comprehension Marko Rauhamaa <marko@pacujo.net> - 2015-08-05 12:01 +0300
Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 19:52 +1000
Re: GOTCHA with list comprehension Marko Rauhamaa <marko@pacujo.net> - 2015-08-05 14:10 +0300
Re: GOTCHA with list comprehension Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2015-08-07 09:08 -0400
Re: GOTCHA with list comprehension Laura Creighton <lac@openend.se> - 2015-08-06 06:39 +0200
Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-06 14:44 +1000
csiph-web