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


Groups > comp.lang.python > #81367

Comprehension with two variables - explanation needed

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 <webmailgroups@gmail.com>
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; '&gt;&gt;&gt;': 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" <webmailgroups@gmail.com>
To <python-list@python.org>
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 <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.16184.1416754678.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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.

 

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.

Back to comp.lang.python | Previous | Next — 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