python中的map
2016-06-03 14:26
253 查看
MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下。
文档中的介绍在这里:
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 function isNone,
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 iterable arguments may be a sequence or any iterable object; the result is always a list.
一点一点看:
1、对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
来个例子:
就像文档中说的:对hh中的元素做了add100,返回了结果的list。
2、如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)
看到并行的效果了吧!在每个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 function isNone,
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 iterable arguments 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’不知道怎么解释,看例子吧
>>> 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)
本文来自于:http://my.oschina.net/zyzzy/blog/115096
我是快乐的搬运工、、、、、、O(∩_∩)O哈哈~
总结一下
第一个参数接收一个函数名,第二个参数接收一个可迭代对象
相关文章推荐
- Python后端【闻宇】
- Python,从 package name 得到 module name
- caffe的python接口封装原理与解析
- python下根据csv将数据集按照类别分文件夹放置
- Python编程规范
- 开发利器之PyCharm
- 【leetcode】3SUM python实现
- 转 python面试题
- python 多线程 入门
- python challenge 13-16关 攻略
- 小强IT游记之新疆行
- python challenge 0-6关 攻略
- python把字典转换成json字符串
- python-module 'pymysql' has no attribute 'connect'
- 解决在Windows server 2008 安装Python2.7版本
- numpy常用函数
- Python configparser模块
- python urllib2
- python爬虫基础A
- python中的@符号的作用