Python中map()函数浅析
2017-06-02 12:33
274 查看
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返回。
来个例子:
就像文档中说的:对hh中的元素做了add100,返回了结果的list。
2、如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)
看到并行的效果了吧!在每个list中,取出了下标相同的元素,执行了abc()。
3、如果'function'给出的是‘None’,自动假定一个‘identity’函数(这个‘identity’不知道怎么解释,看例子吧
)
用语言解释好像有点拗口
,例子应该很容易理解。
介绍到这里应该差不多了吧!不过还有东西可以挖掘:
stackoverflow上有人说可以这样理解map():
赶快试一下:
哦,输出结果一样。原来map()就是列表推导式啊!要是这样想就错了:这里只是表面现象!再来个例子看看:
这个例子我们在上面看过了,若是用列表推导应该怎么写呢?我想是这样的:
但是看到结果,发现根本不是这么回事:
这便是上面列表推导的结果。怎么会这么多?当然了列表推导可以这么写:
原来如此,若是将三个list看做矩阵的话:
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
文档中的介绍在这里:
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 |
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()函数浅析
- 浅析C++中虚函数的调用及对象的内部布局
- .NET 2.0 中 GetDelegateForFunctionPointer 函数实现原理浅析 [草稿]
- 虚函数浅析
- 浅析php中的substr_count()函数的使用
- 浅析μCOS/II v2.85内核OSSemPend()和OSSemPost()函数工作原理
- linux信号机制之sigaction结构体浅析,signal 函数,信号捕捉
- linux信号机制之sigaction结构体浅析,signal 函数,信号捕捉
- ASP.NET文件下载函数使用浅析
- 浅析函数指针
- oracle分析函数浅析
- 浅析C++中虚函数的调用及对象的内部布局2 (来自:lizhe1985)
- VC 函数调用的 汇编代码 浅析
- 浅析函数传参及返回值
- 浅析μCOS/II v2.85内核OSSemPend()和OSSemPost()函数工作原理
- oracle日积月累[ZT]-浅析自动连续数或者按目的取数函数RANK(),DENSE_RANK()功能
- 函数调用的汇编实现浅析
- 浅析C++中虚函数的调用及对象的内部布局1 (来自:lizhe1985)
- linux信号机制之sigaction结构体浅析,signal 函数,信号捕捉
- 浅析μCOS/II v2.85内核OSQPend()和OSQPost()函数工作原理