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


Groups > comp.lang.python > #81374

RE: Comprehension with two variables - explanation needed

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'patterns': 0.04; '21,': 0.07; 'nested': 0.07; 'subject:two': 0.07; 'variables': 0.07; '22,': 0.09; '34,': 0.09; '40,': 0.09; 'explanation': 0.09; 'internet:': 0.09; 'wrong,': 0.09; 'changes': 0.15; 'books': 0.15; '(just': 0.16; '23,': 0.16; '24,': 0.16; '32,': 0.16; '33,': 0.16; '36,': 0.16; '37,': 0.16; '39,': 0.16; '42,': 0.16; '46,': 0.16; '50)': 0.16; '[2,': 0.16; 'awesome.': 0.16; 'brackets': 0.16; 'defined.': 0.16; 'guys,': 0.16; 'instance:': 0.16; 'ivan': 0.16; 'nameerror:': 0.16; 'only)': 0.16; 'skip:[ 60': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'trying': 0.19; 'message-----': 0.19; 'thanks.': 0.20; '>>>': 0.22; 'code,': 0.22; 'example': 0.22; 'print': 0.22; 'error': 0.23; '31,': 0.24; 'comparing': 0.24; 'initial': 0.24; "haven't": 0.24; 'looks': 0.24; "i've": 0.25; '15,': 0.26; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'tried': 0.27; 'idea': 0.28; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; '"",': 0.31; '13,': 0.31; '25,': 0.31; '50,': 0.31; '>>>>': 0.31; 'produces': 0.31; 'yields': 0.31; 'file': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'subject:with': 0.35; 'case,': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'right?': 0.36; 'url:listinfo': 0.36; 'charset:us- ascii': 0.36; 'url:org': 0.36; 'two': 0.37; 'email addr:python.org': 0.37; 'clear': 0.37; 'message-id:@gmail.com': 0.38; 'desirable': 0.38; 'subject:needed': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'skip:- 10': 0.38; 'little': 0.38; 'subject:': 0.39; '12,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'till': 0.61; 'subject:skip:C 10': 0.61; 'range': 0.61; 'first': 0.61; 'making': 0.63; 'name': 0.63; 're:': 0.63; 'decided': 0.64; '(that': 0.65; '30,': 0.65; 'email name:python-list': 0.65; 'to:addr:gmail.com': 0.65; '20,': 0.68; '26,': 0.68; 'received:109': 0.72; 'prime': 0.74; 'square': 0.74; 'now...': 0.84; 'lazy': 0.91; 'hand,': 0.93; '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=zBLx5p381uZcR85/P4fi8DIu34QVnH/Zbp6irkfXJwM=; b=J8tlwk+DRJ3BiDkr5x07Qcv63rfesUcvW08dcIQ3TT8UQGgZgcxz6nq0x18WHJU+Oj FTPwViJn7xiSp779cXInXYJ5HycpyCJ/sMg0oVYnbBHHhxj/93EwgevKu+VzHDI7HF4L wWoZhzWR2hNq2NLOU/u+ORSweaJ6Q9MbpnGnfhxkQ1RJk2501K6gai+j741CqQC6nI2i Q3+0BAA1O2dP1eW66ARFLFoayyN6jXGpJMovDfDVld7KnSxRu+5ph6utFoBMd6wtI90h H7q8pvylPU4JIPYBHpUNg4mWjhqQM/SD4PNF9O2mC949/OD2ngrq7FLJcXWBz8qMetGf 2Q8g==
X-Received by 10.194.222.98 with SMTP id ql2mr26265068wjc.10.1416757112466; Sun, 23 Nov 2014 07:38:32 -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>
In-Reply-To <d1194d28-e9ed-4bd3-86e9-899e13a17097@googlegroups.com>
Subject RE: Comprehension with two variables - explanation needed
Date Sun, 23 Nov 2014 17:37:37 +0200
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding 7bit
X-Mailer Microsoft Outlook 14.0
Thread-Index AQKiD2OX20f6BO7IzFwivzfqqbsyEAEhu3dgmsE0veA=
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.16188.1416757120.18130.python-list@python.org> (permalink)
Lines 112
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416757120 news.xs4all.nl 2913 [2001:888:2000:d::a6]:45059
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81374

Show key headers only | View raw


>Is this what you want?
>
>>>> [[j for j in range(i*2, 50, i)] for i in range(2,8)]
>[[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,
42, 44, 46, 48], [6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48],
[8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48], [10, >15, 20, 25, 30, 35, 40,
45], [12, 18, 24, 30, 36, 42, 48], [14, 21, 28, 35, 42, 49]]

>[I am not sure what you are trying -- just making local changes to the ZF
to make it work]

>https://mail.python.org/mailman/listinfo/python-list

Yes, awesome. Thanks.

But it breaks all the picture that I've built in my head about comps till
now...

As I know from books and googling, the comps main idea looks approximately
like this:

[target <--main loop<--nested loop/s (and maybe some conditions)]    Am I
right? 

But your code looks somehow inverted to me o_0

Like: 

[[target with nested  loop] <--- main loop with initial values for target]

On the other hand, if I'm comparing to my tryouts:

noprimes = [[j for i in range(2, 8)] for j in range(i*2, 50, i)]

It looks same but the variables are not separated, so it definitely  was not
defined....

Sincerely,

Ivan


     




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

On Sunday, November 23, 2014 8:28:16 PM UTC+5:30, Ivan Evstegneev wrote:
> Hello guys,
>  
> I would like to ask you for some explanations on comprehensions. 
> (Don't be scared, it just some particular example ^_^)
>  
> I found this little "find prime number" example over the internet:
>  
> >>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)] 
> >>> primes = [x for x in range(2, 50) if x not in noprimes] print 
> >>> primes
> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
>  
>  
> It looked  pretty clear to me, till the time I decided to make some 
> changes to it %)))
>  
> Ok this is the case: 
>  
> As you can see the first line's comprehension produces a list, that
includes all the multiplications of "i" (that range(2,8) ) in range (i*2,
50, i).
> So  I get this pattern: noprimes  = =  [4, 6, 8.....48, 6, 9,12.... ].
> The change that I tried to apply to this comprehension would lead to 
> the following pattern: noprimes = = [[4, 6,8...48], [6, 9, 12....], 
> ....,[some numbers]]
>  
> But despite my struggling on it for the whole day, I haven't got to 
> the desirable result(using comprehensions only)
>  
> Please, don't get me wrong, I'm not a lazy one (just a beginner). 
>  
> In my case, I  made some googling, and found some patterns for matrices,
that look this one(for example):
>  
> Matrix = [[0 for x in range(5)] for x in range(5)]
>  
> But when I tried to use square brackets with my code, it yields an error
about definition of "i", for instance:
>  
> File "<pyshell#30>", line 1, in <module> noprimes = [[j for i in 
> range(2, 8)] for j in range(i*2, 50, i)]
> NameError: name 'i' is not defined.

Is this what you want?

>>> [[j for j in range(i*2, 50, i)] for i in range(2,8)]
[[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,
42, 44, 46, 48], [6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48],
[8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48], [10, 15, 20, 25, 30, 35, 40,
45], [12, 18, 24, 30, 36, 42, 48], [14, 21, 28, 35, 42, 49]]

[I am not sure what you are trying -- just making local changes to the ZF to
make it work]
--
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