转 python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
2013-05-28 23:01
1056 查看
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')]
另外一篇内置函数实现的解释
filter内建函数的python实现:
>>> def filter(bool_func,seq):
>>>filtered_seq = []
>>>for eachItem in seq:
>>> if bool_func(eachItem):
>>> filtered_seq.append(eachItem)
>>>return filtered_seq
2、
map内建函数的python实现:
>>> def map(func,seq):
>>>mapped_seq = []
>>>for eachItem in seq:
>>> mapped_seq.append(func(eachItem))
>>>return mapped_seq
3、
reduce的python实现:
>>> def reduce(bin_func,seq,initial=None):
>>>lseq = list(seq)
>>>if initial is None:
>>> res = lseq.pop(0)
>>>else:
>>> res = initial
>>>for eachItem in lseq:
>>> res = bin_func(res,eachItem)
>>>return res
>>>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')]
另外一篇内置函数实现的解释
filter内建函数的python实现:
>>> def filter(bool_func,seq):
>>>filtered_seq = []
>>>for eachItem in seq:
>>> if bool_func(eachItem):
>>> filtered_seq.append(eachItem)
>>>return filtered_seq
2、
map内建函数的python实现:
>>> def map(func,seq):
>>>mapped_seq = []
>>>for eachItem in seq:
>>> mapped_seq.append(func(eachItem))
>>>return mapped_seq
3、
reduce的python实现:
>>> def reduce(bin_func,seq,initial=None):
>>>lseq = list(seq)
>>>if initial is None:
>>> res = lseq.pop(0)
>>>else:
>>> res = initial
>>>for eachItem in lseq:
>>> res = bin_func(res,eachItem)
>>>return res
相关文章推荐
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python几个重要的函数(lambda,filter,reduce,map,zip) 一、匿名函数lambda lambda argument1,argument2,...argumentN :e
- Python学习笔记(5):lambda,filter,map,reduce,zip用法整理
- python几个重要的函数(lambda,filter,reduce,map,zip)
- Python中几个操作列表的内置函数filter(),map(),reduce(),lambda
- Python常用内置函数整理(lambda,reduce,zip,filter,map)
- python几个内置函数之-filter,map,reduce
- (转)Python中的map,reduce,filter,lambda内置函数
- python 中的 filter, lambda, map, reduce 内置函数
- python特殊用法:filter map reduce lambda
- Python 中的 map, reduce, zip, filter, lambda基本使用方法
- Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- python 中的 filter, lambda, map, reduce 内置函数
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
- Python中filter、map、reduce、lambda 的用法
- python 内置函数filter, lambda, map, reduce
- Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- 【Python】Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- Python中filter、map、reduce、lambda 的用法