2019-04-12
阅读量:
503
解释一些在Python中实现面向功能的编程的方法
有时,当我们想要遍历列表时,一些方法会派上用场。
1)filter()
过滤器允许我们根据条件逻辑过滤一些值。
list(filter(lambda x:x> 5,range(8)))
[6,7]
2)map()
Map将函数应用于iterable中的每个元素。
list(map(lambda x:x ** 2,range(8)))
[0,1,4,9,16,25,36,49]
3)reduce()
在我们达到单个值之前,Reduce会反复减少序列顺序。
from functools import reduce
reduce(lambda x,y:xy,[1,2,3,4,5])
-13






评论(0)


暂无数据
推荐帖子
2条评论
6条评论
7条评论