MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下。

文档中的介绍在这里:

map(functioniterable...)

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()函数浅析的更多相关文章

  1. Python中map函数

    1.简介 python 提供内置函数map(), 接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回.例如: (1)对于list [1, 2 ...

  2. python中map()函数的用法讲解

    map函数的原型是map(function, iterable, -),它的返回结果是一个列表. 参数function传的是一个函数名,可以是python内置的,也可以是自定义的. 参数iterabl ...

  3. Python中yield函数浅析

    带有yield的函数在Python中被称之为generator(生成器),下面我们将使用斐波那契数列来举例说明下该函数:(环境是在Python3.x下)  如何生成斐波那契数列: 斐波那契(Fibon ...

  4. python中map()函数

    map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. map()是 Python 内 ...

  5. python中map函数和reduce函数的区别

    ①从参数方面来讲:map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数.reduce()函数 ...

  6. python中map函数的用法

    map函数类似一个生成器 具体用例如下: def add(x): a =[,,] b = map(add,[,,]) print( list(map(add,[,,])) ) print(b,type ...

  7. 【转】Python 中map、reduce、filter函数

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  8. Python中int()函数的用法浅析

      int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int)  Help on class int in module __builti ...

  9. Python 中的函数

    学了 Python 中的数据类型,语句,接下来就来说一下 Python 中的函数,函数是结构化编程的核心.我们使用函数可以增加程序的可读性.自定义函数时使用关键字def 函数由多条语句组成.在定义函数 ...

随机推荐

  1. Ajax异步请求模板

    $.ajax({ url: '', type: 'post', data: {'id':id}, dataType: 'json', success: function(data,statusText ...

  2. IT行业有前景么?一个10年行内人的6点看法

    本人毕业快11年了. 大学读的建筑专业,却在IT行业干了10年. 真心来讲,我非常感谢好兄弟老唐,是他在我迷茫的那两年,领着我踏入了IT行业,也找到了自己的兴趣爱好. 这些年我经常在知乎.博客等地方发 ...

  3. 微软为啥让免费升Win10?

           今天终于赶在截止日期之前把我的联想PC升到win10.微软这次对中国开放的持续一年的免费升级活动主要有两个原因.首先当然是"感恩Windows用户长久支持的回馈".微 ...

  4. 【Alpha阶段】第七次scrum meeting

    一.会议照片 二.会议内容 姓名 学号 负责模块 昨日任务 今日任务 杨爱清 099 界面设计和交互功能 [完成]设计界面 交互功能连接并优化 杨立鑫 100 数据库搭建和其他 [完成]将数据库与其他 ...

  5. 201521123100 《Java程序设计》第4周学习总结

    1. 本章学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. (1)多态性:相同的形态,不同的行为 (2)类型转换与强制类型转换(cast) 2. 书面作 ...

  6. 戴建钊 201521123023《Java程序设计》第2周学习总结

    1. 本周学习总结 (1)String类:字符串连接"+",以前觉得方便但不知其原理,所以在有大量修改字符串操作的时候用得不亦乐乎,既浪费内存,又减缓效率.现在知道用Stringb ...

  7. JS运算符的一些简单练习和应用

    练习-01    判断奇数偶数           var num =prompt("请输入一个数");                                    al ...

  8. [07] String字符串

    1.相同又不同的字符串 String str1 = new String("String"); String str2 = "String"; String s ...

  9. JSP页面格式化数字或时间 基于jstl的

    jsp页面格式化数字或时间 转载自: http://blog.csdn.net/hakunamatata2008/archive/2011/01/21/6156203.aspx Tags fmt:re ...

  10. CacheConcurrencyStrategy五种缓存方式

    CacheConcurrencyStrategy有五种缓存方式:  CacheConcurrencyStrategy.NONE,不适用,默认  CacheConcurrencyStrategy.REA ...