Python中map()函数
2015-07-27 10:29
711 查看
文档中的介绍在这里:
map(function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must
take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended withNoneitems. If functionisNone,
the identity function is assumed; if there are multiple arguments, map() returns
a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterablearguments may be a sequence or any iterable object; the result is always a list.
一点一点看:
1、对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
2、如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)
>>> 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、如果'function'给出的是‘None’,自动假定一个‘identity’函数(这个‘identity’不知道怎么解释,看例子吧)
map(function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must
take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended withNoneitems. If functionisNone,
the identity function is assumed; if there are multiple arguments, map() returns
a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterablearguments may be a sequence or any iterable object; the result is always a list.
一点一点看:
1、对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
>>> def add100(x): ... return x+100 ... >>> hh = [11,22,33] >>> map(add100,hh) [111, 122, 133]就像文档中说的:对hh中的元素做了add100,返回了结果的list。
2、如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)
>>> 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、如果'function'给出的是‘None’,自动假定一个‘identity’函数(这个‘identity’不知道怎么解释,看例子吧)
相关文章推荐
- 命令行2048
- Windows下Python包和模块的安装方法
- 正则
- Sublime2下配置Python模块查找路径PYTHONPATH
- Python—global vs local variables
- Python中特殊函数集锦
- python用于测试的failIf,failUnless
- Python logging模块详解
- Python - 命令行参数
- 【python】hust virtual judge 交题记录抓取
- 获取分享社最新一期的迅雷分享账号
- 用Python模拟浏览器登录
- python 爬图 helloworld
- Python中特殊函数集锦
- 在Python中的Django框架中进行字符串翻译
- 在Python的Django框架中创建语言文件
- Python多线程结合队列下载百度音乐的方法
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- python数组过滤实现方法
- python内建函数a-b部分(一)