Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'patterns': 0.04; 'i?m': 0.05; 'nested': 0.07; 'result,': 0.07; 'subject:two': 0.07; 'internet:': 0.09; 'wrong,': 0.09; 'changes': 0.15; '(just': 0.16; '23,': 0.16; '37,': 0.16; '42,': 0.16; '50)': 0.16; '[2,': 0.16; 'any)?': 0.16; 'brackets': 0.16; 'defined.': 0.16; 'get,': 0.16; 'guys,': 0.16; 'haven?t': 0.16; 'instance:': 0.16; 'mean,': 0.16; 'nameerror:': 0.16; 'number?': 0.16; 'only)': 0.16; 'looked': 0.18; 'all,': 0.19; 'not,': 0.20; 'solution.': 0.20; '>>>': 0.22; 'code,': 0.22; 'example': 0.22; 'print': 0.22; 'error': 0.23; '>>>': 0.24; '31,': 0.24; 'initial': 0.24; "haven't": 0.24; '15,': 0.26; 'this:': 0.26; 'tried': 0.27; 'don?t': 0.29; 'moved': 0.30; "i'm": 0.30; '"",': 0.31; '13,': 0.31; '50,': 0.31; 'produces': 0.31; 'yields': 0.31; 'file': 0.32; 'skip:& 30': 0.33; 'problem': 0.35; 'subject:with': 0.35; 'case,': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'url:rec-html40': 0.35; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'possible': 0.36; 'url:org': 0.36; 'skip:4 10': 0.37; 'url:microsoft': 0.37; 'clear': 0.37; 'message- id:@gmail.com': 0.38; 'desirable': 0.38; 'subject:needed': 0.38; 'url:office': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'url:schemas': 0.38; 'little': 0.38; 'url:omml': 0.39; 'url:2004': 0.39; 'skip:& 20': 0.39; '12,': 0.39; 'bad': 0.39; 'realize': 0.39; 'url:12': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:n 30': 0.60; 'solve': 0.60; 'till': 0.61; 'subject:skip:C 10': 0.61; 'range': 0.61; 'first': 0.61; 'name': 0.63; 'such': 0.63; 'decided': 0.64; 'different': 0.65; '(that': 0.65; 'skip:r 40': 0.68; 'fact,': 0.69; 'applying': 0.72; 'received:109': 0.72; 'further,': 0.74; 'prime': 0.74; 'square': 0.74; 'hand': 0.80; '^_^': 0.84; 'lazy': 0.91; 'ultimate': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:mime-version:content-type :thread-index:content-language; bh=AuZ6MFc+u7oDxO75t52K2fLEKu/2bQtEV2SEV3xgKwA=; b=cQzjusr3/PWKh1ZgRw5TxzfxqFYYWuO4qGKKhiJU67uKBTMgl9RA45FSY16n8eeNBG DhIeaEdKMgPmMQtAaMGnXKthug+6ydpBbl8O0bc5ZxdHVTp8AQd58BMAdq39AQzFvWop dVHuWqrg+nUDnOEdj1jdxfxKhsVZ1azZlav1o3lgvWpRVYTPxh5vWhCFBlCkSe02PUNc dTxhF21tAtXG0dPGNf0Ewmyem/4zXGnBYtEuwqvd3Lmo9QaLz9U1O5+kj1SwGyM4FhQ9 av+MrPu1Q1qvzYI+yEt+h6LdesX1xrVjgUmehAqTn4Ntf/qpY4VhcsJZ49VHZ3ZQs8e8 xlEA== X-Received: by 10.194.248.162 with SMTP id yn2mr25629155wjc.16.1416754670054; Sun, 23 Nov 2014 06:57:50 -0800 (PST) From: "Ivan Evstegneev" To: Subject: Comprehension with two variables - explanation needed Date: Sun, 23 Nov 2014 16:56:55 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0021_01D0073E.7D4009E0" X-Mailer: Microsoft Outlook 14.0 Thread-Index: AdAHJ07C+x0MZQuQSU6dWzH7/3SkNw== 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 423 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416754678 news.xs4all.nl 2850 [2001:888:2000:d::a6]:50325 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81367 This is a multipart message in MIME format. ------=_NextPart_000_0021_01D0073E.7D4009E0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 "", line 1, in noprimes = [[j for i in range(2, 8)] for j in range(i*2, 50, i)] NameError: name 'i' is not defined. Then I tried to apply those brackets to "j", like this: >>> noprimes = [[j] for i in range(2, 8) for j in range(i*2, 50, i)] In this case I get, pretty predictable result, like this: [[num], [num], [num],...,[num]], and this is still not what I'm looking for.. So I moved further, and succeeded to solve my problem in a different manner: >>> noprimes=[] >>> for i in range(2,8): noprimes.append(list(range(i*2,50,i))) Which yields, in fact, the matrix I wanted from the beginning: noprimes == [[4, 6, 8, 10, 12,.., 48], [6, 9, 12, 15, 18, ., 42, 45, 48],..,[numbers]] Yep, this the "classical" way for me, on the other hand I have the feeling, that this is not, the ultimate solution. ^_^ So my questions is still persists: 1) Is it possible to realize such a thing by applying changes only to initial comprehension? If yes, it would be nice to see the solution. 2) How actually bad is my solution? I mean, after all, there are nested functions call in my code, so how much the speed is affected(if any)? Thanks a lot in advance, Ivan. ------=_NextPart_000_0021_01D0073E.7D4009E0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

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 =3D [j for i in = range(2, 8) for j in range(i*2, 50, i)= ]
>>> primes =3D [x for x in ra= nge(2, 50) if x not in noprimes]
>>> print primes

[2, 3, 5, 7, 11, 13, 17,&= nbsp;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  =3D =3D  [4, 6, 8…..48, 6, = 9,12…. ].

The change that I = tried to apply to this comprehension would lead to the following = pattern: noprimes =3D =3D [[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 =3D = [[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 =3D [[j for i = in range(2, 8)] for j in range(i*2, 50, i)]

NameError: name 'i' is not defined.

 

Then I tried = to apply those brackets to “j”, like this:

 

>>> noprimes =3D [[j] for i in&nbs= p;range(2, 8) for j in range(i*2, 50, = i)]

 

In this case I get, pretty predictable result, like = this: [[num], [num], [num],…..,[num]], and  this is still not = what I’m looking for….

 

So I moved = further, and succeeded to solve my problem in a different = manner:

 

>>> noprimes=3D[]

>>> for i in = range(2,8):

noprimes.append(list(range(i*= 2,50,i)))

 

Which yields, in fact, the matrix I wanted from the = beginning:

 

noprimes =3D=3D [[4, 6, 8, 10, 12,…., 48], [6, = 9, 12, 15, 18, …, 42, 45, = 48],……,[numbers]]

Yep, this the “classical” way for me, on = the other hand I have the feeling, that this is not, the ultimate = solution. ^_^

 

So my questions is still persists:

 

1)      = Is it possible to realize = such a thing by applying changes only to initial comprehension? If yes, = it would be nice to see the solution.

2)      = How actually bad is my = solution? I mean, after all, there are nested functions call in my code, =  so how much the speed is affected(if any)?

 

Thanks a lot = in advance,

 

Ivan.

------=_NextPart_000_0021_01D0073E.7D4009E0--