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返回。
来个例子:
就像文档中说的:对hh中的元素做了add100,返回了结果的list。
2、如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)
2 |
... return
a*10000
+ b*100
+ c |
7 |
>>> map(abc,list1,list2,list3) |
8 |
[114477,
225588,
336699] |
看到并行的效果了吧!在每个list中,取出了下标相同的元素,执行了abc()。
3、如果'function'给出的是‘None’,自动假定一个‘identity’函数(这个‘identity’不知道怎么解释,看例子吧
)
7 |
>>> map(None,list1,list2,list3) |
8 |
[(11,
44, 77), (22,
55, 88), (33,
66, 99)] |
用语言解释好像有点拗口
,例子应该很容易理解。
介绍到这里应该差不多了吧!不过还有东西可以挖掘:
stackoverflow上有人说可以这样理解map():
5 |
[f(x) for
x in
iterable] |
赶快试一下:
8 |
>>> [add100(i) for
i in
list1] |
哦,输出结果一样。原来map()就是列表推导式啊!要是这样想就错了:这里只是表面现象!再来个例子看看:
2 |
... return
a*10000
+ b*100
+ c |
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] |
这便是上面列表推导的结果。怎么会这么多?当然了列表推导可以这么写:
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、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- python基础——map/reduce
python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 使用Python实现Map Reduce程序
使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...
- python中map()和dict()的用法
map()用法 map()是python的内置函数,会根据提供的函数对指定序列做映射. 语法: map(func, iter, ...) 其中func为一个功能函数,iter表示可迭代参数序列.map ...
- Python【map、reduce、filter】内置函数使用说明
题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...
- 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()函数使用举例 # 功能: ...
- python之map、filter、reduce、lambda函数
map map函数根据提供的函数对指定的序列做映射,定义:map(function, sequence[,sequence,...])--->list 例1 >>> map(l ...
- Python的map、filter、reduce函数 [转]
1. map函数func作用于给定序列的每个元素,并用一个列表来提供返回值. map函数python实现代码: def map(func,seq): mapped_seq = [] fo ...
随机推荐
- C/C++静态数组与动态数组的区别
简介 以下三行代码有什么区别? int a[10]; int *a = (int*)malloc(sizeof(int)*10); int *a = new int[10]; 第一行代码定义a为包含1 ...
- 编写高性能的Lua代码
编写高性能的Lua代码 Posted on2014/04/18· 10 Comments 前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件, ...
- 拾遗与填坑《深度探索C++对象模型》3.3节
<深度探索C++对象模型>是一本好书,该书作者也是<C++ Primer>的作者,一位绝对的C++大师.诚然该书中也有多多少少的错误一直为人所诟病,但这仍然不妨碍称其为一本好书 ...
- Activtiy完全解析(三、View的显示过程measure、layout、draw)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52840065 本文出自:[openXu的博客] 在Activity完全解析的第一篇文章A ...
- Linux 高性能服务器编程——Linux服务器程序规范
问题聚焦: 除了网络通信外,服务器程序通常还必须考虑许多其他细节问题,这些细节问题涉及面逛且零碎,而且基本上是模板式的,所以称之为服务器程序规范. 工欲善其事,必先利其器,这篇主要来探 ...
- Dynamics CRM EXCEL导入数据字段类型为选项集时的注意事项
在开始先展示下CRM的导入数据涉及选项集字段时的一个问题 下图是选项集字段的属性 下图是我要导入的excel中的列值,可以看出列明和字段名是一致的,而列值却不是选项集中已有的选项 在导入校验时,只要字 ...
- java基础知识——网络编程、IO流
IO流 字节流:处理字节数据的流对象,计算机中最小数据单元就是字节.InputStream OutputStream 字符流:字符编码问题,将字节流和编码表封装成对象就是字符流.Reader Writ ...
- Findbug在项目中的运用--提高代码质量
FindBugs是一个静态分析工具,它检查类或者 JAR文件,将字节码与一组缺陷模式进行对比以发现可能的问题.有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析 第一 手动安装 在Ec ...
- 剑指Offer——巧妙使用sort(List<T>,Comparator<? super T>)比较器
剑指Offer--巧妙使用sort(List<T>,Comparator<? super T>)比较器 先入为主 package cn.edu.ujn.offersword; ...
- Linux之read命令使用
read命令: read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 1)read后面的变量var可以只有一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个 ...