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,就写到这里。仅个人理解,如有差错请指正,多谢!

上面的例子有些来自于这里:

python中的map()函数的更多相关文章

  1. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下   Py ...

  2. 实现python中的map函数

    假设Python没有提供map()函数,自行编写my_map()函数实现与map()相同的功能.以下代码在Python 2.7.8中实现. 实现代码: def my_map(fun,num): i = ...

  3. Python 中的map函数,filter函数,reduce函数

      自学python,很多地方都需要恶补.       三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str.   1.map函数 map函数会根据提供的函数对指定序 ...

  4. python中的map函数

    def f(x): return x * x """将一个全是数字的list变成平方形式""" def f2(): ls = [1, 2, ...

  5. python 中的map 详解

    python中的map函数应用于每一个可迭代的项,返回的是一个结果list.如果有其他的可迭代参数传进来,map函数则会把每一个参数都以相应的处理函数进行迭代处理.map()函数接收两个参数,一个是函 ...

  6. python学习之map函数和reduce函数的运用

    MapReduce:面向大型集群的简化数据处理引文 map()函数 Python中的map()函数接收两个参数,一个是调用函数对象(python中处处皆对象,函数未实例前也可以当对象一样调用),另一个 ...

  7. python中 Lambda,Map,Filter,Itertools,Generator高级函数的用法

    Lambda 函数 Lambda 函数是一种比较小的匿名函数--匿名是指它实际上没有函数名. Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda ...

  8. Python连载43-current中的map函数、xml文件

    一.current中的map函数 1.map(fn,*iterable,timeout=None) (1)跟map函数相类似(2)函数需要异步执行(3)timeout代表超时时间 (4)map和sub ...

  9. python中使用zip函数出现<zip object at 0x02A9E418>

    在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...

随机推荐

  1. 安卓解析 json 4种格式 全解析

    总共4种json  ,    还提供了 httclient  和 httpurlconnection 2种方式获取json的方法(请把baseurl 改为可以地址比如http://www.sohu.c ...

  2. Decorator - 装饰模式

    1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法,或者须要给方法添加更多的功能(魅力),你也许会仅仅继 ...

  3. JavaScript Interview Questions: Event Delegation and This

    David Posin helps you land that next programming position by understanding important JavaScript fund ...

  4. phpexcel单元格内换行

    我说的这个换行不是字多了,自动换行的那种,是在特定位置添加换行符 代码如下: $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A4 ...

  5. 为 Blade 模板引擎添加新文件扩展名

    因为一些原因,我准备把 Blessing Skin 的框架换成 Laravel 了(之前是自己搭建的一个框架),但是在模板迁移的时候遇到了一点问题. 之前我是使用的 XiaoLer/blade 这个从 ...

  6. cocos2dx 3.x fullPathForFilename及isFileExist在ios/mac下与win32下行为不同

    一,fullPathForFilename 项目先开发了ios/mac版本,这两天想把win32工程也配好,但遇到了部分资源无法正确找到的问题. 进一步观察发现,对于那些找不到的资源路径,fullPa ...

  7. 练习 Dijkstra 最短路径算法。

    练习 Dijkstra 最短路径算法. #coding: utf-8 # Author: woodfox, Oct 14, 2014 # http://en.wikipedia.org/wiki/Di ...

  8. Java 并发多线程处理优秀博文整理

    多线程(11)-Fork/Join-Java并行计算框架 推荐理由:Java在JDK7之后加入了并行计算的框架Fork/Join,本文是对其讲解 分解和合并:Java 也擅长轻松的并行编程! 推荐理由 ...

  9. CF 366E - Dima and Magic Guitar 最远曼哈顿距离

    题目:http://codeforces.com/problemset/problem/366/E 事实上就是找 n * m 矩阵中数字 x 和 数字 y 的最远距离. 方法參照武森的论文<浅谈 ...

  10. css 图标 旋转中

    .person-loading .loading-icon{position: absolute;top: 14px;} i.loading-icon{width: 15px;height: 15px ...