python 如此灵活的使用filter, map, reduce, sum
Functional Programming Tools
There are three built-in functions that are very useful when used with lists: filter(), map(), and reduce().
filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true. If sequence is a string or tuple, the result will be of the same type; otherwise, it is always a list. For example, to compute some primes:
>>> def sum(seq):... def add(x,y): return x+y... return reduce(add, seq, 0)...>>> sum(range(1, 11))55>>> sum([])0