Python中map()函数浅析
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返回。
来个例子:
>>> 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)]
用语言解释好像有点拗口
,例子应该很容易理解。
介绍到这里应该差不多了吧!不过还有东西可以挖掘:
stackoverflow上有人说可以这样理解map():
map(f, iterable)
基本上等于:
[f(x) for x in iterable]
赶快试一下:
>>> def add100(x):
... return x + 100
...
>>> list1 = [11,22,33]
>>> map(add100,list1)
[101, 102, 103]
>>> [add100(i) for i in list1]
[101, 102, 103]
哦,输出结果一样。原来map()就是列表推导式啊!要是这样想就错了:这里只是表面现象!再来个例子看看:
>>> 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]
这个例子我们在上面看过了,若是用列表推导应该怎么写呢?我想是这样的:
[abc(a,b,c) for a in list1 for b in list2 for c in list3]
但是看到结果,发现根本不是这么回事:
[114477, 114488, 114499, 115577, 115588, 115599, 116677, 116688, 116699, 224477, 224488, 224499, 225577, 225588, 225599, 226677, 226688, 226699, 334477, 334488, 334499, 335577, 335588, 335599, 336677, 336688, 336699]
这便是上面列表推导的结果。怎么会这么多?当然了列表推导可以这么写:
result = []
for a in list1:
for b in list2:
for c in list3:
result.append(abc(abc))
原来如此,若是将三个list看做矩阵的话:
| 11 | 22 | 33 |
| 44 | 55 | 66 |
| 77 | 88 | 99 |
map()只做了列上面的运算,而列表推导(也就是嵌套for循环)做了笛卡尔乘积。
OK,就写到这里。仅个人理解,如有差错请指正,多谢!
上面的例子有些来自于这里:
http://infohost.nmt.edu/tcc/help/pubs/python/web/map-function.html
http://stackoverflow.com/questions/10973766/understanding-the-map-function-python
Python中map()函数浅析的更多相关文章
- Python中map函数
1.简介 python 提供内置函数map(), 接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回.例如: (1)对于list [1, 2 ...
- python中map()函数的用法讲解
map函数的原型是map(function, iterable, -),它的返回结果是一个列表. 参数function传的是一个函数名,可以是python内置的,也可以是自定义的. 参数iterabl ...
- Python中yield函数浅析
带有yield的函数在Python中被称之为generator(生成器),下面我们将使用斐波那契数列来举例说明下该函数:(环境是在Python3.x下) 如何生成斐波那契数列: 斐波那契(Fibon ...
- python中map()函数
map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. map()是 Python 内 ...
- python中map函数和reduce函数的区别
①从参数方面来讲:map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数.reduce()函数 ...
- python中map函数的用法
map函数类似一个生成器 具体用例如下: def add(x): a =[,,] b = map(add,[,,]) print( list(map(add,[,,])) ) print(b,type ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- Python中int()函数的用法浅析
int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builti ...
- Python 中的函数
学了 Python 中的数据类型,语句,接下来就来说一下 Python 中的函数,函数是结构化编程的核心.我们使用函数可以增加程序的可读性.自定义函数时使用关键字def 函数由多条语句组成.在定义函数 ...
随机推荐
- python-分页代码
page.py ''' django内使用方式: all_count = models.UserInfo.objects.all().count() # path_info 当前页的url # all ...
- 201521123044 《Java程序设计》第6周学习总结
1. 本章学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123065《java程序设计》第9周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 出现的异 ...
- Eclipse rap 富客户端开发总结(8) : 发布到tomcat后解决rap编码和字符集的问题
1 .解决 rap 字符集乱码的问题 字符集问题,解决办法: 在plugin.xml - build.properties 中添加 javacDefaultEncoding.. = UTF-8 ...
- python基础之socket
一.osi七层 完整的计算机系统由硬件,操作系统,软件组成. 互联网的核心就是由一堆协议组成,协议就是标准,如全世界通信的标准就是英语. 如果把计算机比作人,那么互联网协议就是计算机界的英语,所有计算 ...
- webservice第一篇【介绍、Scoket、http调用、wsimport调用】
WebService介绍 首先我们来谈一下为什么需要学习webService这样的一个技术吧-. 问题一 如果我们的网站需要提供一个天气预报这样一个需求的话,那我们该怎么做????? 天气预报这么一个 ...
- 基于FPGA的腐蚀膨胀算法实现
本篇文章我要写的是基于的腐蚀膨胀算法实现,腐蚀膨胀是形态学图像处理的基础,,腐蚀在二值图像的基础上做"收缩"或"细化"操作,膨胀在二值图像的基础上做" ...
- 我的Java设计模式-工厂方法模式
女朋友dodo闹脾气,气势汹汹的说"我要吃雪糕".笔者心里啊乐滋滋的,一支雪糕就能哄回来,不亦乐乎?! 但是,雪糕买回来了,她竟然说"不想吃雪糕了,突然想吃披萨" ...
- git fatal: I don't handle protocol 'https'问题的解决
问题重现 新建的仓库,再把本地的代码往上push的时候Git提示 $ fatal: I don't handle protocol 'https' 问题分析 Git是支持https的,这点毋庸置疑,所 ...
- New Features and changes of Ninject 3.3
Ninject 3.3 beta1 has gone live. This release mainly focus on bug fix and platform update. Support . ...