Nastiest Python list comprehension ever
It's a buggy implementation of primes_below(n) through a sieve.>>> def mystery(n):"There should be one - and preferably only one - obvious way to do it." And this ain't it.
> 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)
Can somebody from the Perl community please but this guy in the right place by supplying a one-liner Perl golf quadruple map call?
I think it's a testimony to the readability of Python that I've never even programed in the language, nor am I familiar with the finer points of the list syntax (a[::2]?!), and yet this mucked up piece of shit nonetheless gave me the first impression of a prime number sieve. I actually still don't understand how the sieve works, but it somehow just looks like one. Or is it just the case that assuming every obfuscated loop is a prime number sieve is disproportionately likely to be true?
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!
Corrected version:
def mystery(n): a = list(range(n)) return [[i for a[::i] in [([0]*n)[::i]]][0] for i in a[2:] if a[i]]Isn't the point of a list comprehension - comprehension?
(It's a paraphrase of something Feynman once said to Murray Gell-Mann.)
Wow. I hadn't realized that syntax like "for a[::i] in" is valid Python. Attribute assignment (for x.foo in) seem to work too. I'm not sure if there will ever be any reason to use those in real code, but it's nice trivia to know for Python golfing.
This is why moving away from higher order functions and towards list comprehensions is a mistake.
Bad BDFL, bad!
undefined
My favorite to date is:
bps = [map(int, bps.split('/')) if isinstance(bps, str) else bps for bps in bps if bps]
The clause "else bps for bps in bps if bps" made me incredibly incredibly happy.