Python 中的 map, reduce, zip, filter, lambda基本使用方法
2014-02-27 12:26
1181 查看
map(function, sequence[, sequence, ...]
该函数是对sequence中的每个成员调用一次function函数,如果参数有多个,则对每个sequence中对应的元素调用function。
返回值为一个列表
如: def func(x): return x + 10;
map(func, [1,2,3])
输出为[11,12,13]
又如: def func(x, y) : return x * 10 + y;
map(func, [1,2], [3,4])
输出为: [13, 24]
第一个例子又可以写成:
map(lambda x : x + 10, [1, 2, 3])
第二个例子可以写成:
map(lambda x, y : x * 10 + y, [1,2], [3,4])
这就是lambda的基本使用方法,常用来定义短小的函数。
reduce(function, sequence[, init]);
如果没有init, reduce第一次是对sequence中的前两个元素调用function, 之后每次使用上一次返回的结果和sequnce中的下一个元素来调用function. 这里function是一个二元函数
如: reduce (lambda x, y : x + y, [1, 2, 3])
结果为: 6
又如:reduce (lambda x, y : x + y, [1,2,3], 1)
结果为:7
filter(function or None, sequnce);
这里的function是一个布尔函数,filter对每个sequnce中的元素调用function, 返回结果为真的元素,如果sequnce是tuple,string, 那么返回类型也为tuple或string, 否则返回一个list
zip(seq1 [, seq2 [...]])
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th
element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.
该函数是对sequence中的每个成员调用一次function函数,如果参数有多个,则对每个sequence中对应的元素调用function。
返回值为一个列表
如: def func(x): return x + 10;
map(func, [1,2,3])
输出为[11,12,13]
又如: def func(x, y) : return x * 10 + y;
map(func, [1,2], [3,4])
输出为: [13, 24]
第一个例子又可以写成:
map(lambda x : x + 10, [1, 2, 3])
第二个例子可以写成:
map(lambda x, y : x * 10 + y, [1,2], [3,4])
这就是lambda的基本使用方法,常用来定义短小的函数。
reduce(function, sequence[, init]);
如果没有init, reduce第一次是对sequence中的前两个元素调用function, 之后每次使用上一次返回的结果和sequnce中的下一个元素来调用function. 这里function是一个二元函数
如: reduce (lambda x, y : x + y, [1, 2, 3])
结果为: 6
又如:reduce (lambda x, y : x + y, [1,2,3], 1)
结果为:7
filter(function or None, sequnce);
这里的function是一个布尔函数,filter对每个sequnce中的元素调用function, 返回结果为真的元素,如果sequnce是tuple,string, 那么返回类型也为tuple或string, 否则返回一个list
zip(seq1 [, seq2 [...]])
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th
element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.
相关文章推荐
- Python中map,reduce,filter和sorted函数的使用方法
- Python中filter、lambda、map、reduce的使用
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
- Python中map,reduce,filter和sorted函数的使用方法
- python几个重要的函数(lambda,filter,reduce,map,zip)
- Python高阶函数(Map、Reduce、Filter)和lambda函数一起使用 ,三剑客
- python中的zip、map、reduce 、lambda函数的使用。
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- Pythont特殊语法filter,map,reduce,apply使用方法
- Python list--> Filter , Map , Reduce 使用笔记 (lambda )
- Python常用内置函数整理(lambda,reduce,zip,filter,map)
- 【python】详解lambda匿名函数以及结合map、reduce、filter、sorted等使用
- Python学习笔记(5):lambda,filter,map,reduce,zip用法整理
- python中map、filter、reduce 函数使用方法
- Python3 zip,lambda,map,reduce的使用
- python几个重要的函数(lambda,filter,reduce,map,zip) 一、匿名函数lambda lambda argument1,argument2,...argumentN :e
- 转 python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- Python中的特殊语法:filter、map、reduce、lambda介绍