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


Groups > comp.lang.python > #81394

RE: Comprehension with two variables - explanation needed

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <webmailgroups@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.025
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'puts': 0.07; 'subject:two': 0.07; 'variables': 0.07; '"if': 0.09; 'explanation': 0.09; 'def': 0.12; 'language.': 0.14; '"list': 0.16; '(assuming': 0.16; '(output': 0.16; '23,': 0.16; 'comp': 0.16; 'complicated,': 0.16; 'covert': 0.16; 'docs.': 0.16; 'doing,': 0.16; 'least)': 0.16; 'pity': 0.16; 'roy': 0.16; 'skip:[ 60': 0.16; 'student,': 0.16; 'sugar': 0.16; 'syntactic': 0.16; 'true:': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'message-----': 0.19; 'seems': 0.21; 'saying': 0.22; 'skip': 0.24; "i've": 0.25; 'header :In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'leave': 0.29; 'especially': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'easier': 0.31; 'url:wiki': 0.31; '(perhaps': 0.31; 'url:wikipedia': 0.31; 'url:python': 0.33; 'guess': 0.33; 'not.': 0.33; 'problem': 0.35; 'subject:with': 0.35; 'something': 0.35; 'form.': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'too': 0.37; 'two': 0.37; 'list': 0.37; 'email addr:python.org': 0.37; 'message-id:@gmail.com': 0.38; 'subject:needed': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'little': 0.38; 'does': 0.39; 'subject:': 0.39; 'functional': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'major': 0.40; 'url:mail': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'till': 0.61; 'mentioned': 0.61; 'subject:skip:C 10': 0.61; "you're": 0.61; 'first': 0.61; 're:': 0.63; 'more': 0.64; 'email name:python-list': 0.65; 'finally': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'natural': 0.68; 'smith': 0.68; 'nos': 0.70; 'received:109': 0.72; 'here...': 0.84; 'now...': 0.84; 'technically': 0.84; 'terrible': 0.84; 'defeat': 0.91; 'picture': 0.97
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-type:content-transfer-encoding:thread-index :content-language; bh=PWz0zSgp4QXBgxkxxzKCeSAhXeg3peHQgJwLV7K3d8k=; b=lEJI7zF3O0BVD9lJPrMVhEzQy/5CgHKICHA6HeMMNFcC855XlJx4q/XawrkBy+yy4L DVlZD6T+UbM63+g2PQv62PzHTvSc+/lzyCsM+dbPAlHPqnFwTTqCXbrcxqCRtpwLNZHi 7qwzJ6cNM/YwXm4d/KIrBnHNpmnsdC5LluCkpHALclgIeZkItTjOKk5bCkqB9ManoV26 6IaVJgfota6gAxk7kAwcloHnQEfoqtA1zdvGVlN+YcXooJQRMR7Vqqyvx4xVq5If/+ce rD0x2be66+jvYClSh4xCfbbk/a2nafYgFLFYls/4oqmkrymxxvexNSjTKhEe7pSceToU IOxw==
X-Received by 10.180.81.102 with SMTP id z6mr14346999wix.69.1416763677616; Sun, 23 Nov 2014 09:27:57 -0800 (PST)
From "Ivan Evstegneev" <webmailgroups@gmail.com>
To "'Rustom Mody'" <rustompmody@gmail.com>, <python-list@python.org>
References <mailman.16184.1416754678.18130.python-list@python.org> <d1194d28-e9ed-4bd3-86e9-899e13a17097@googlegroups.com> <002501d00733$6995dce0$3cc196a0$@gmail.com> <mailman.16191.1416757555.18130.python-list@python.org> <roy-B11F64.10563923112014@news.panix.com> <07f25264-1286-4d98-820c-949dba711248@googlegroups.com>
In-Reply-To <07f25264-1286-4d98-820c-949dba711248@googlegroups.com>
Subject RE: Comprehension with two variables - explanation needed
Date Sun, 23 Nov 2014 19:27:02 +0200
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding 7bit
X-Mailer Microsoft Outlook 14.0
Thread-Index AQKiD2OX20f6BO7IzFwivzfqqbsyEAEhu3dgApVOyQcDKb4z/AIzSlJRAuCtSPGaar+AIA==
Content-Language en-us
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.16201.1416764650.18130.python-list@python.org> (permalink)
Lines 91
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416764650 news.xs4all.nl 2912 [2001:888:2000:d::a6]:58796
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81394

Show key headers only | View raw


Got the main idea. 
Still need to "chew it up"  in depth. (As I mentioned I'm a beginner.... EE
student, so please excuse me for my  "lameness"  ^_^)

-----Original Message-----
From: Python-list
[mailto:python-list-bounces+webmailgroups=gmail.com@python.org] On Behalf Of
Rustom Mody
Sent: Sunday, November 23, 2014 18:46
To: python-list@python.org
Subject: Re: Comprehension with two variables - explanation needed

On Sunday, November 23, 2014 9:27:22 PM UTC+5:30, Roy Smith wrote:
>  Skip Montanaro wrote:
> 
> > > But it breaks all the picture that I've built in my head about 
> > > comps till now...
> > 
> > Note that list comprehensions are little more than syntactic sugar 
> > for for loops. If you're having terrible writing or understanding 
> > one, especially a compound one like your example, it can help to 
> > write it as a (nested) for loop, then covert it (perhaps incrementally)
to the list comp form.
> 
> Or not.  If it was complicated enough that you needed to loopify it to 
> understand what it's doing, have pity on the next person who has to 
> maintain your code and leave it as a loop :-)
> 
> My general rule for code hygiene is, "If I have to think about whether 
> something is too complicated, it is".

[I guess I am in the minority here... anyways... here goes]

1. I find comprehensions are harder than for-loops -- corollary to
functional code is easier than imperative -- corollary to time is a major
headache. If the problem does not involve time the solution should try to
avoid it

2. "List comprehensions are syntactic sugar for for loops"
Cannot technically quarrel with that as that is the position of the official
docs.
However to me it puts the cart before the horse.
Its like saying that 

def foo(x): return x+1

is just the same as

              0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (1)
              6 BINARY_ADD
              7 RETURN_VALUE


(output of dis)

That may be the case but it seems (to me at least) to defeat the purpose of
using an hi-level language.

3. So how should one think of list comprehensions?
   As a python/executable version of 'set-builder notation'
   http://en.wikipedia.org/wiki/Set-builder_notation

4. Finally to the OP (assuming you wanted a prime-number generator) Heres a
solution for your consideration.

First a one-line solution in haskell

sieve (p:xs)   =    p:sieve [x | x <- xs, x `mod` p /= 0]

Call with sieve [2..]

Now a pythonification of that

def n2s():  # natural nos from 2; ie [2..]
    i=2
    while True:
        yield i
        i+=1

def sieve(l):
    p = next(l)
    yield p
    yield from sieve (x for x in l if x % p != 0)


Call as
for p in sieve(n2s()): print(p)
--
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


Thread

Comprehension with two variables - explanation needed "Ivan Evstegneev" <webmailgroups@gmail.com> - 2014-11-23 16:56 +0200
  Re: Comprehension with two variables - explanation needed Rustom Mody <rustompmody@gmail.com> - 2014-11-23 07:09 -0800
    RE: Comprehension with two variables - explanation needed "Ivan Evstegneev" <webmailgroups@gmail.com> - 2014-11-23 17:37 +0200
    Re: Comprehension with two variables - explanation needed Chris Angelico <rosuav@gmail.com> - 2014-11-24 02:42 +1100
    RE: Comprehension with two variables - explanation needed Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-23 09:45 -0600
      Re: Comprehension with two variables - explanation needed Roy Smith <roy@panix.com> - 2014-11-23 10:57 -0500
        Re: Comprehension with two variables - explanation needed Rustom Mody <rustompmody@gmail.com> - 2014-11-23 08:45 -0800
          Re: Comprehension with two variables - explanation needed Rustom Mody <rustompmody@gmail.com> - 2014-11-23 09:17 -0800
          RE: Comprehension with two variables - explanation needed "Ivan Evstegneev" <webmailgroups@gmail.com> - 2014-11-23 19:27 +0200
          Re: Comprehension with two variables - explanation needed Steven D'Aprano <steve@pearwood.info> - 2014-11-24 04:42 +0000
            Re: Comprehension with two variables - explanation needed Chris Angelico <rosuav@gmail.com> - 2014-11-24 15:51 +1100
              Re: Comprehension with two variables - explanation needed Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-25 09:20 +1100
            Re: Comprehension with two variables - explanation needed Rustom Mody <rustompmody@gmail.com> - 2014-11-23 22:47 -0800
            Re: Comprehension with two variables - explanation needed Dan Stromberg <drsalists@gmail.com> - 2014-11-24 16:42 -0800
        Re: Comprehension with two variables - explanation needed Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-23 10:45 -0600
        Re: Comprehension with two variables - explanation needed Roy Smith <roy@panix.com> - 2014-11-23 12:08 -0500
        Re: Comprehension with two variables - explanation needed Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2014-11-23 17:46 +0000

csiph-web