[PEP] matching and mismatching
Mark McEahern
marklists at mceahern.com
Thu May 2 12:10:07 EDT 2002
[Luke Kenneth Casson]
> the idea: a function/operator similar to map and reduce _or_ a keyword
> similar to "in", such that:
>
> for x in ([1,3,2,5,9] matching lambda x:x > 2):
> print x
> will output:
> 3
> 5
> 9
What's wrong with using list comprehensions? E.g.,
>>> nums = range(1, 11)
>>> odds = [x for x in nums if x % 2]
>>> evens = [x for x in nums if not (x % 2)]
>>> nums
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> odds
[1, 3, 5, 7, 9]
>>> evens
[2, 4, 6, 8, 10]
Cheers,
// mark
More information about the Python-list
mailing list