python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
来源:互联网 发布:牛仔外套品牌推荐知乎 编辑:程序博客网 时间:2024/06/04 23:25
1.lambda lambda其实就是一条语句,lambda(x):body。x是lambda函数的参数,参数可以有任意多个(包括可选参数);body是函数体,只能是一个表达式,并且直接返回该表达式的值。
>>>f=lambda x:x+1
>>>f(2)
3
>>>(lambda x,y:x+y)(2,3)
5
2.filter filter(func, list)接受两个参数:一个函数func和一个列表list,返回一个列表。函数func只能有一个参数。filter的功能:列表中所有元素作为参数传递给函数,返回可以另func返回真的元素的列表
>>>l=['abc','acd','1245','ddad','aaa']
>>>func(s)
... return s.startswith('a')
>>>filter(func, l)
['abc','acd','aaa']
filter为过滤list,并返回list,绑定的函数为一个返回bool值的函数
filter(lambda item:item>2,[1,2,3,4])
>>>[3,4]
def fun(x):
return x>2 and x<6
list=[1,2,3,4,5,6,7]
filter(fun,list)
>>> [3,4,5]
3.zip zip函数接受任意多个序列作为参数,将所有序列按相同的索引组合成一个元素是各个序列合并成的tuple的新序列,新的序列的长度以参数中最短的序列为准。另外(*)操作符与zip函数配合可以实现与zip相反的功能,即将合并的序列拆成多个tuple
>>>x=[1,2,3],y=['a','b','c']
>>>zip(x,y)
[(1,'a'),(2,'b'),(3,'c')]
>>>zip(*zip(x,y))
[(1,2,3),('a','b','c')]
4.map为操作list,返回list,绑定的函数为修改list中每一个值的函数
>>> list=[1,2,3]
>>> map(lambda x : x*2,list)
>>> [2, 4, 6]
5.reduce为逐次操作list里的每项,接收的参数为 2个,最后返回的为一个结果
>>> def myadd(x,y):
>>> return x+y
>>> sum=reduce(myadd,(1,2,3))
>>> 6
http://www.cnblogs.com/phonlin/archive/2010/02/05/1664688.html
http://www.cnblogs.com/lanstonpeng/articles/1875727.html
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip filter )用法
- python几个重要的函数(lambda,filter,reduce,map,zip) 一、匿名函数lambda lambda argument1,argument2,...argumentN :e
- python 内置函数filter, lambda, map, reduce
- Python 内置函数 lambda、filter、map、reduce
- python内置函数lambda、filter、map、reduce
- python的lambda、filter、map、reduce、zip函数
- (转)Python中的map,reduce,filter,lambda内置函数
- Python函数式编程-map()、zip()、filter()、reduce()、lambda()
- python 中的 filter, lambda, map, reduce 内置函数
- python 中的 filter, lambda, map, reduce 内置函数
- [Python] 内置序列函数之 filter, map, reduce, zip
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python内置高阶函数map,reduce,filter,sorted,zip
- 短信的收发及在android模拟器之间实践
- 上次使用Chronometer这个控件实现了秒表,这次使用handler实现秒表(完结)
- Recipe 4.10. Adding an Entry to a Dictionary
- POJ2251 BFS基础题
- Android Service学习之本地服务
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- Win7 字体不得不说之事
- 让你的FireFox支持迅雷精简版
- HTML头部,元信息
- 模板缓冲区的使用
- 家事国事我调侃
- 深入理解iPhone委托模式兼谈iPhone生命周期
- 怎样使UISearchBar背景透明
- c# web 用户控件的使用