python内置函数lambda、filter、map、reduce
2017-12-01 15:34
621 查看
1、lambda——为了减少单行函数的定义而存在的
返回函数对象,经常与其余四个内置函数一起使用
2、filter——filter(function, sequence)
sequence中的每一项执行function过滤,所以function需要返回True/False
3、map——map(function, sequence)
sequence中的每一项执行function方法,返回列表
4、reduce——reduce(function, sequence [, init])
sequence中的每一项迭代执行function方法,init为初始值,可以省略。所以function需要有两个参数~~~
返回函数对象,经常与其余四个内置函数一起使用
func = lambda x: x+1 ||#相同 def func(x): return x+1 func(1)=2
2、filter——filter(function, sequence)
sequence中的每一项执行function过滤,所以function需要返回True/False
# 对于[2,3,4,5]中的每一项,执行lambda x: x%2==0,判断是否为偶数,输出偶数项。 filter(lambda x: x%2==0, range(2,6)) = [2, 4]
3、map——map(function, sequence)
sequence中的每一项执行function方法,返回列表
# 对于"abc"中的每一项,执行加法,输出。 map(lambda x: x+x, "abc") = ['aa', 'bb', 'cc'] map(lambda x: x+x, (1,2,3)) = [2, 4, 6] map(lambda x, y: x*y, [1, 2, 3], [4, 5, 6]) = [4, 10, 18] # func为None时,下边这两个效果是一样的。 map(None, [1, 2, 3], [4, 5, 6]) = [(1, 4), (2, 5), (3, 6)] zip([1, 2, 3], [4, 5, 6]) = [(1, 4), (2, 5), (3, 6)]
4、reduce——reduce(function, sequence [, init])
sequence中的每一项迭代执行function方法,init为初始值,可以省略。所以function需要有两个参数~~~
reduce(lambda x,y: x+y, [1, 3, 5, 7, 9]) = 25 reduce(lambda x,y: x+y, [1, 3, 5, 7, 9], 100) = 125
相关文章推荐
- python 中的 filter, lambda, map, reduce 内置函数
- Python 内置函数 lambda、filter、map、reduce
- Python常用内置函数整理(lambda,reduce,zip,filter,map)
- 转 python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 中的 filter, lambda, map, reduce 内置函数
- (转)Python中的map,reduce,filter,lambda内置函数
- python 内置函数filter, lambda, map, reduce
- Python中几个操作列表的内置函数filter(),map(),reduce(),lambda
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python高级特性: 函数编程 lambda, filter,map,reduce
- Python特殊语法:filter、map、reduce、lambda
- Python list--> Filter , Map , Reduce 使用笔记 (lambda )
- python几个内置函数之-filter,map,reduce
- python中filter, map, reduce, lambda
- Python之filter、map、reduce、lambda
- 【ZZ】Python特殊语法:filter、map、reduce、lambda
- [转] Python特殊语法:filter、map、reduce、lambda
- Python内置函数之map()、reduce()、filter()函数