map(function,
iterable,
...)

将function放到迭代的每个元素执行,结果为list。

引自》:http://my.oschina.net/zyzzy/blog/115096

文档中的介绍在这里:

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返回。

来个例子:

1 >>> def
add100(x):
2 ... return
x+100
3 ...
4 >>> hh =
[11,22,33]
5 >>> map(add100,hh)
6 [111,
122, 133]

就像文档中说的:对hh中的元素做了add100,返回了结果的list。

2、如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)

1 >>> def
abc(a, b, c):
2 ... return
a*10000
+ b*100
+ c
3 ...
4 >>> list1 =
[11,22,33]
5 >>> list2 =
[44,55,66]
6 >>> list3 =
[77,88,99]
7 >>> map(abc,list1,list2,list3)
8 [114477,
225588,
336699]

看到并行的效果了吧!在每个list中,取出了下标相同的元素,执行了abc()。

3、如果'function'给出的是‘None’,自动假定一个‘identity’函数(这个‘identity’不知道怎么解释,看例子吧

1 >>> list1 =
[11,22,33]
2 >>> map(None,list1)
3 [11,
22, 33]
4 >>> list1 =
[11,22,33]
5 >>> list2 =
[44,55,66]
6 >>> list3 =
[77,88,99]
7 >>> map(None,list1,list2,list3)
8 [(11,
44, 77), (22,
55, 88), (33,
66, 99)]

用语言解释好像有点拗口 ,例子应该很容易理解。

介绍到这里应该差不多了吧!不过还有东西可以挖掘:

stackoverflow上有人说可以这样理解map():

1 map(f, iterable)
2  
3 基本上等于:
4  
5 [f(x) for
x in
iterable]

赶快试一下:

1 >>> def
add100(x):
2 ... return
x +
100
3 ...
4 >>> list1 =
[11,22,33]
5 >>> map(add100,list1)
6 [101,
102, 103]
7  
8 >>> [add100(i) for
i in
list1]
9 [101,
102, 103]

哦,输出结果一样。原来map()就是列表推导式啊!要是这样想就错了:这里只是表面现象!再来个例子看看:

1 >>> def
abc(a, b, c):
2 ... return
a*10000
+ b*100
+ c
3 ...
4 >>> list1 =
[11,22,33]
5 >>> list2 =
[44,55,66]
6 >>> list3 =
[77,88,99]
7 >>> map(abc,list1,list2,list3)
8 [114477,
225588,
336699]

这个例子我们在上面看过了,若是用列表推导应该怎么写呢?我想是这样的:

1 [abc(a,b,c) for
a in
list1
for b
in list2
for c in
list3]

但是看到结果,发现根本不是这么回事:

1 [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]

这便是上面列表推导的结果。怎么会这么多?当然了列表推导可以这么写:

1 result =
[]
2  
3 for
a
in list1:
4 for
b in
list2:
5 for
c in
list3:
6 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、reduce、filter】内置函数使用说明(转载)

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

  2. python基础——map/reduce

    python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...

  3. python之map、filter、reduce、lambda函数 转

    python之map.filter.reduce.lambda函数  转  http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...

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

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

  5. 使用Python实现Map Reduce程序

    使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...

  6. python中map()和dict()的用法

    map()用法 map()是python的内置函数,会根据提供的函数对指定序列做映射. 语法: map(func, iter, ...) 其中func为一个功能函数,iter表示可迭代参数序列.map ...

  7. Python【map、reduce、filter】内置函数使用说明

    题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...

  8. python之map

    python之Map函数   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # map()函数使用举例 # 功能: ...

  9. python之map、filter、reduce、lambda函数

    map map函数根据提供的函数对指定的序列做映射,定义:map(function, sequence[,sequence,...])--->list 例1 >>> map(l ...

  10. Python的map、filter、reduce函数 [转]

    1. map函数func作用于给定序列的每个元素,并用一个列表来提供返回值. map函数python实现代码: def map(func,seq): mapped_seq = []        fo ...

随机推荐

  1. Docker有用的资源

    资源链接 Docker 主站点: https://www.docker.io Docker 注册中心API: http://docs.docker.com/reference/api/registry ...

  2. Java对象的创建 —— new之后JVM都做了什么?

    Java对象创建过程 1. 类加载检查 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没 ...

  3. Apache shiro集群实现 (八) web集群时session同步的3种方法

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  4. java虚拟机 jvm java堆 方法区 java栈

    java堆是java应用程序最密切的内存空间.几乎所有的对象都存在堆中.java堆完全自动化管理,通过垃圾回收机制,垃圾对象会自动清理,不需要显式释放. 根据java垃圾回收机制的不同,java堆可能 ...

  5. Android 深入理解Loader机制 让APP轻装上阵

    本文简书同步发布,谢谢关注. http://blog.csdn.net/sk719887916/article/details/51540610 Android开发者都经历过APP UI开发不当 会造 ...

  6. JQuery之DOM操作及常用函数

    属性操作 attr(name)获取属性值 var imgSrc = $("img").attr("src") attr(name,value)设置属性值 $(& ...

  7. ROS_Kinetic_23 ROS流行版本和相关书籍汇总

    目前,ROS使用的主流版本主要是下面四种:Hydro,Indigo,Jade,Kinetic. Distro Release date Poster Tuturtle, turtle in tutor ...

  8. An universal algorithm design of fixed length substring locating

         An universal algorithm design of fixed length substring locating Stringlocating is a very commo ...

  9. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(五)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 为了暂时不影响原来的cat移动方法,我们在CatSprite.m ...

  10. 5个你不知道的HTML5的接口

    原文地址:5 HTML5 APIs You Didn't Know Existed 原文日期: 2010年09月27日 翻译日期: 2013年8月7日 当人们看到或者说出"HTML5&quo ...