Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81367
| From | "Ivan Evstegneev" <webmailgroups@gmail.com> |
|---|---|
| Subject | Comprehension with two variables - explanation needed |
| Date | 2014-11-23 16:56 +0200 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16184.1416754678.18130.python-list@python.org> (permalink) |
[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
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