Python 中的 map, reduce, zip, filter, lambda基本使用方法
来源:互联网 发布:烟台玉兔网络 编辑:程序博客网 时间:2024/06/05 15:36
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.
- Python 中的 map, reduce, zip, filter, lambda基本使用方法
- python中的函数:filter/map/reduce/lambda
- Python 中的 lambda,filter,map,reduce,apply
- python的lambda、filter、map、reduce、zip函数
- Python函数式编程-map()、zip()、filter()、reduce()、lambda()
- python filter lambda reduce map
- Python: lambda, map, reduce, filter
- Python filter、map、reduce、lambda
- Python:lambda、map、filter、reduce
- filter、map、reduce、lambda(Python)
- python 中的 filter, lambda, map, reduce 内置函数
- python中的map/reduce, filter, lambda, set的用法
- python中的函数编程:lambda,apply, filter,map,reduce
- python 中的 filter, lambda, map, reduce 内置函数
- python中的map,reduce,filter,sorted,lambda表达式
- (转)Python中的map,reduce,filter,lambda内置函数
- Python中的map、zip、filter、reduce函数使用
- Python特殊语法:filter、map、reduce、lambda
- C#版本socket聊天软件
- 服务器证书安装配置指南(Weblogic)
- SQL 异常处理 Begin try end try begin catch end catch
- Lucene与Solr的比较
- 服务器证书安装配置指南(Jboss)
- Python 中的 map, reduce, zip, filter, lambda基本使用方法
- Codeforces 397A
- Codeforces 397B
- 服务器证书安装配置指南(IIS7.0)
- windows开发命令大全
- 快结束了
- TCP/IP 分段与重组
- 服务器证书安装配置指南(Nginx)
- mahout相似度算法(一)