Re: Comprehension with two variables - explanation needed

2014-11-24 Thread Dan Stromberg
On Sun, Nov 23, 2014 at 8:42 PM, Steven D'Aprano wrote: > On Sun, 23 Nov 2014 08:45:39 -0800, Rustom Mody wrote: >> First a one-line solution in haskell >> >> sieve (p:xs) =p:sieve [x | x <- xs, x `mod` p /= 0] > > Don't use that! That is a horribly inefficient way to generate primes. > > Ma

Re: Comprehension with two variables - explanation needed

2014-11-24 Thread Steven D'Aprano
Chris Angelico wrote: > On Mon, Nov 24, 2014 at 3:42 PM, Steven D'Aprano > wrote: >> Today dis() returns the above, tomorrow it may return: >> >> 0 LOAD_GLOBAL 0 (x) >> 3 INCREMENT >> 5 RETURN_VALUE >> >> (say), and the Python code remains th

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Rustom Mody
On Monday, November 24, 2014 10:13:04 AM UTC+5:30, Steven D'Aprano wrote: > On Sun, 23 Nov 2014 08:45:39 -0800, Rustom Mody wrote: > > First a one-line solution in haskell > > > > sieve (p:xs) =p:sieve [x | x <- xs, x `mod` p /= 0] > > Don't use that! That is a horribly inefficient way to g

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Chris Angelico
On Mon, Nov 24, 2014 at 3:42 PM, Steven D'Aprano wrote: > Today dis() returns the above, tomorrow it may return: > > 0 LOAD_GLOBAL 0 (x) > 3 INCREMENT > 5 RETURN_VALUE > > (say), and the Python code remains the same even though the byte code i

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Steven D'Aprano
On Sun, 23 Nov 2014 08:45:39 -0800, Rustom Mody wrote: > 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

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Jon Ribbens
On 2014-11-23, Roy Smith wrote: > In article , > 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 under

RE: Comprehension with two variables - explanation needed

2014-11-23 Thread Ivan Evstegneev
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

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Rustom Mody
On Sunday, November 23, 2014 10:15:51 PM UTC+5:30, Rustom Mody wrote: > 1. I find comprehensions are harder than for-loops -- Heh! Meant to say 'easier' of course... -- https://mail.python.org/mailman/listinfo/python-list

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Roy Smith
We seem to be somewhat more liberal about nested comprehensions here, but I can't say I'm proud of that :-) 906 Python source files, 109k lines. $ find . -name '*.py' | xargs grep '\[.* for .*\]' | wc -l 729 $ find . -name '*.py' | xargs grep '\[.* for .* for .*\]' | wc -l 46 Without naming n

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Rustom Mody
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

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Skip Montanaro
On Sun, Nov 23, 2014 at 9:57 AM, Roy Smith wrote: > > 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 Well, sure. I was mostly trying to give Ivan a path out of the weed

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Roy Smith
In article , 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

RE: Comprehension with two variables - explanation needed

2014-11-23 Thread Skip Montanaro
> 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 (

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Chris Angelico
On Mon, Nov 24, 2014 at 2:37 AM, Ivan Evstegneev wrote: > 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: > >

RE: Comprehension with two variables - explanation needed

2014-11-23 Thread Ivan Evstegneev
Ivan -Original Message- From: Python-list [mailto:python-list-bounces+webmailgroups=gmail@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, Novem

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Chris Angelico
On Mon, Nov 24, 2014 at 1:56 AM, 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

Re: Comprehension with two variables - explanation needed

2014-11-23 Thread Rustom Mody
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: > > >>>

Comprehension with two variables - explanation needed

2014-11-23 Thread Ivan Evstegneev
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