python函数式编程filter,reduce
过滤出奇数:
1 >>> list(filter(lambda x:x%2,range(10))) 2 [1, 3, 5, 7, 9]
累加:
1 >>> from functools import reduce 2 >>> reduce(lambda x,y:x+y,(1,2,3,4)) 3 10
自定义reduce:
>>> def myreduce(func,seq): total=seq[0] for next in seq[1:]: total=func(total,next) return total >>> myreduce(lambda x,y:x+y,(1,3,2,5)) 11 >>> myreduce(lambda i,j:i*j,[3,4,5]) 60