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’函数

>>> 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看做矩阵的话: map()只做了列上面的运算,而列表推导(也就是嵌套for循环)做了笛卡尔乘积。

最新文章

  1. Java接口响应超时监控
  2. 中文的加密传输(python版)
  3. UIView和CALayer的区别
  4. 用Gen8服务器来学习虚拟化ESXI
  5. elasticsearch配置
  6. xcode6默认不支持armv7s
  7. win7中USB音箱没有声音解决的方法
  8. C# IO操作(四)大文件拷贝(文件流的使用)、文件编码
  9. activity的生命周期详解
  10. Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .
  11. 使用ionic与cordova(phonegap)进行轻量级app开发前的环境配置与打包安卓apk过程记录
  12. 剑指Offer——小米+小红书笔试题+知识点总结
  13. Python的GIL机制与多线程编程
  14. Android开发 - 掌握ConstraintLayout(九)分组(Group)
  15. 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page
  16. 7.纯 CSS 创作一个 3D 文字跑马灯特效
  17. PS故障风海报制作技术分享
  18. poj 3258 3273
  19. pyspider示例代码七:自动登陆并获得PDF文件下载地址
  20. 【转】基于R语言构建的电影评分预测模型

热门文章

  1. python使用socket向客户端发送数据的方法
  2. Lua实现简单的类,继承,多态 实例
  3. “但行好事 莫问前程 只问耕耘 不问收获 成功不必在我 而功力必不唐捐” 科技袁人·年终盛典——5G是科技时代非常重要的基础设施
  4. GxDlms编译
  5. 冲刺Noip2017模拟赛7 解题报告——五十岚芒果酱
  6. linux之dup和dup2函数解析
  7. 《xv6 Appendices: PC Hardware and Boot loader》学习笔记
  8. poj3347(扩大数据,避免小数)
  9. 【AtCoder】ARC065
  10. MySQL create table语法详解