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. 预处理指令--C语言

    ANSI标准C还定义了如下几个宏: __LINE__ 表示正在编译的文件的行号 __FILE__ 表示正在编译的文件的名字 __DATE__ 表示编译时刻的日期字符串,例如:"25 Dec ...

  2. Objective-C语法概述

    Objective-C语法概述 简称OC 面向对象的C语言 完全兼容C语言 可以在OC里面混入C/C++代码 可以开发IOS和Mac OS X平台应用 语法预览 关键字 基本上都是以@开头(为了与C语 ...

  3. Android Studio 如何打JAR包

    Android Studio 如何打JAR包 在eclipse中我们知道如何将一个项目导出为jar包,供其它项目使用.  在AS中可以通过修改gradle才处理.  我们新建一个项目MakeJar,在 ...

  4. GitLab服务器IP地址设置

    最近使用GitLab 搭建了Git的私有仓库,但是发现私有仓库的地址居然是localhost,不是本机的IP地址,最后百度了一下,找了很久才找到,特此记录一下. 首先说明一下,我linux虚拟机的IP ...

  5. ICL Auto Vectorization

    简介 此文简单介绍如何使用intel c++编译器实现向量化加速. 全文如下安排: base : 待优化的源代码. vectorization : 第一个向量化版本. aligned : 内存对其对向 ...

  6. MT8127:如何让system分区可读写(MTK安卓6.0)

    Android 系统默认情况下,system 分区是只读 mount 的,因为无法进行往里写数据的,可 以用 adb 命令 adb remount 重新 mount 一下. 也可以通过在板子上,输入以 ...

  7. ELK平台的搭建

    ELK是指Elasticsearch + Logstash + Kibaba三个组件的组合.本文讲解一个基于日志文件的ELK平台的搭建过程,有关ELK的原理以及更多其他信息,会在接下来的文章中继续研究 ...

  8. 开源框架Volley的使用《二》[NetWorkImageView&&LruCache&ImageLoader]

    转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客    http://blog.csdn.net/qq_32059827/article/details/5278849 ...

  9. Swift基础之守卫语句guard

    本篇文章翻译自:http://ericcerney.com/swift-guard-statement/原作者:ecerney该语法为swift2.0之后添加的新特性 最开始在Apple的Platfo ...

  10. 22 Notification 通知栏代码

    结构图: MainActivity.java package com.qf.day22_notification; import android.app.Activity; import androi ...