Live data from Hacker News

Nastiest Python list comprehension ever

blog.garlicsim.org

1–10 of 55 posts

Re: Nastiest Python list comprehension ever

#4

Does that even run? :) Nested comprehensions are one of the things I hate about Python actually. The functions in itertools are so much clearer. I can't really think of a good reason to use nested comprehensions... they become unreadable really fast!

The nesting of list comprehension is just an aperitif in this function.

Re: Nastiest Python list comprehension ever

#6
post #5

> Does anyone have a suggestion on how to do it? Yes, provide more details so I don't have to spend twenty minutes figuring out what it does. (Going by my three-second-look gut feeling: It's the Sieve of Eratosthenes)

it generates a list of prime numbers up to 'n', including some false positives.

The author wasn't kidding about it being nasty to read...

Re: Nastiest Python list comprehension ever

#7
post #5

> Does anyone have a suggestion on how to do it? Yes, provide more details so I don't have to spend twenty minutes figuring out what it does. (Going by my three-second-look gut feeling: It's the Sieve of Eratosthenes)

Looking at it a bit more, it's sort-of-obvious that that's what it is, if you've seen the Sieve before. However:

> i for a[::i] in

Mind blown. How does assigning to an array even work? I'm guessing this is way-undefined-speak for setting elements, and I am simultaneously dazzled by its brilliance and horrified by its stupidity.

Re: Nastiest Python list comprehension ever

#8
post #7
post #5

> Does anyone have a suggestion on how to do it? Yes, provide more details so I don't have to spend twenty minutes figuring out what it does. (Going by my three-second-look gut feeling: It's the Sieve of Eratosthenes)

Looking at it a bit more, it's sort-of-obvious that that's what it is, if you've seen the Sieve before. However: > i for a[::i] in Mind blown. How does assigning to an array even work? I'm guessing this is way-undefined-speak for setting elements, and I am simultaneously dazzled by its brilliance and horrified by its stupidity.

Yes, that was the desired effect :)

Re: Nastiest Python list comprehension ever

#10
post #5

> Does anyone have a suggestion on how to do it? Yes, provide more details so I don't have to spend twenty minutes figuring out what it does. (Going by my three-second-look gut feeling: It's the Sieve of Eratosthenes)

Yes, but I could not understand why one needs

  list(range(n)) as opposed to range(n)
and why the double indexing of a in

  i in a[:][2:] if a[i] == i].
Am I missing something ? I think it would work without those.

Edit: Yo La Tengo. Deviousness aside the code is quite nice. It would be fun to optimize it but without breaking its spirit.

Post reply on HN