对Python中map()函数的理解
2016-07-10 20:27
477 查看
在python2.6.6文档中的描述:
Help on built-in function map in module __builtin__:
map(...)
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s).
If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length.
If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
1、对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
来个例子:
2、如果给出了一个以上的可迭代参数,函数调用与包含每个可迭代参数中对应项的参数列表,当所给的可迭代参数不是有相同的长度的话就用None替代缺失的值。(翻译的有点拗口)
3、如果函数是‘None’,则返回一个可迭代参数项的列表(或者,如果给出的可迭代参数多于一个,则返回元组的列表)
原帖出处:http://my.oschina.net/zyzzy/blog/115096
Help on built-in function map in module __builtin__:
map(...)
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s).
If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length.
If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
1、对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
来个例子:
>>> def add100(x):... return x+100... >>> hh = [11,22,33] >>> map(add100,hh) [111, 122, 133]就像文档中说的:对hh中的元素做了add100,返回了结果的list。
2、如果给出了一个以上的可迭代参数,函数调用与包含每个可迭代参数中对应项的参数列表,当所给的可迭代参数不是有相同的长度的话就用None替代缺失的值。(翻译的有点拗口)
>>> def abc(a, b, c): ... return a*10000 + b*100 + c... >>> list1 = [11,22,33] >>> list2 = [44,55,66] >>> list3 = [77,88,99] >>> map(abc,list1,list2,list3) [114477, 225588, 336699]看到并行的效果了吧!在每个list中,取出了下标相同的元素,执行了abc()。
3、如果函数是‘None’,则返回一个可迭代参数项的列表(或者,如果给出的可迭代参数多于一个,则返回元组的列表)
>>> list1 = [11,22,33] >>> map(None,list1) [11, 22, 33] >>> list1 = [11,22,33] >>> list2 = [44,55,66] >>> list3 = [77,88,99] >>> map(None,list1,list2,list3) [(11, 44, 77), (22, 55, 88), (33, 66, 99)]
# 可以用列表方法来对高阶函数进行更巧妙的引用 [add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]
原帖出处:http://my.oschina.net/zyzzy/blog/115096
相关文章推荐
- 【Python学习笔记】函数式编程:高阶函数map和reduce
- Python 数据抓取之正则表达式
- 【Python学习笔记】函数式编程:高阶函数
- python filter,map,lambda
- 【Python学习笔记】函数式编程
- python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?
- python多进程队列的2种实现方法
- Python中iter函数用法简述
- Python KeyError
- python算法——字符串表达式的计算
- Notepad++下Python的自动补全
- 编码和Python的bytearray , bytes
- 深入RandomFroest
- K均值聚类算法(K-Means)
- python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决
- Python学习笔记9:Python文件和输入输出
- Python自动化运维开发之paramiko(远程批量管理服务器)
- 【机器学习实验】scikit-learn的主要模块和基本使用
- 关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化
- 批量替换文件夹名称或文件名称中特定字符