List Comprehensions Enhancement
Hannah Schroeter
hannah at schlund.de
Mon Sep 13 09:32:47 EDT 1999
Hello!
In article <37D9E932.66F2B0D3 at cosc.canterbury.ac.nz>,
Greg Ewing <greg at cosc.canterbury.ac.nz> wrote:
>[...]
>print [3 * x for x in nums]
>print [x for x in nums if x > 2]
>print [(i, s) for i in nums for s in strs]
>print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
The syntax looks a bit verbose, compared to e.g. Haskell's list
comprehension syntax which looks quite similar to the mathematical set
comprehensions:
print [3 * x | x <- nums]
print [x | x <- nums, x > 2]
print [(i,s) | i <- nums, s <- strs]
print [(i,s) | i <- nums, s <- [f | f <- strs, 'n' `elem` f]]
Also, I think there should be a clear separator between the
generators/predicates in your syntax too:
print [(i,s) for i in nums, for s in strs]
or similar.
Nice thing, though :-)
Regards, Hannah.
More information about the Python-list
mailing list