python内置函数lambda、filter、map、reduce
来源:互联网 发布:各种排序算法 编辑:程序博客网 时间:2024/05/19 12:39
1、lambda——为了减少单行函数的定义而存在的
返回函数对象,经常与其余四个内置函数一起使用
func = lambda x: x+1||#相同def func(x): return x+1func(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]) = 25reduce(lambda x,y: x+y, [1, 3, 5, 7, 9], 100) = 125
阅读全文
0 0
- python 内置函数filter, lambda, map, reduce
- Python 内置函数 lambda、filter、map、reduce
- python内置函数lambda、filter、map、reduce
- python 中的 filter, lambda, map, reduce 内置函数
- python 中的 filter, lambda, map, reduce 内置函数
- (转)Python中的map,reduce,filter,lambda内置函数
- [Python] 函数lambda(), filter(), map(), reduce()
- [Python] 函数lambda(), filter(), map(), reduce()
- python中的函数:filter/map/reduce/lambda
- python函数lambda(), filter(), map(), reduce()
- [Python] 函数lambda(), filter(), map(), reduce()
- python内置函数map/reduce/filter
- python内置函数map/reduce/filter
- python map ,reduce filter内置函数使用说明
- python 内置函数map/filter/reduce
- python 内置函数 map、 reduce 、filter
- python内置函数map/reduce/filter
- Python【map、reduce、filter】内置函数使用说明
- XMind,让你的双十二亮起来
- 所有面积公式
- iOS中代理属性为什么要用Weak修饰?
- 【Android学习】Window和WindowManager
- hdoj 6231 K-th Number
- python内置函数lambda、filter、map、reduce
- ROS中rviz添加urdf文件显示机器人模型
- 因为user中文名称造成的错误解决方法 错误集合001
- C++ 互斥量的封装
- java 垃圾回收
- python 判断数据类型
- 《大话设计模式》之代理模式
- Vue-cli项目中,引入css、js或者引入组件,检查后未发现错误,npm红叉报错原因
- Shell监控服务器状态