Python map()

这里的map可不是google的mapreduce模型,而是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 with None items. If function is None, 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

大概意思就是将function方法作用于iterable中的每一个元素,结果作为list输出

>>> def fun(a):
…     return a + 10

>>> map(fun, [1, 2, 3])
[11, 12, 13]
>>>
即将[1, 2, 3]中每个元素都做了fun,结果用列表给出返回值

>>> def fun(a, b):
…     return a + b * 10

>>> list1 = [1, 2, 3]
>>> list2 = [4, 5, 6]
>>> map(fun, list1, list2)
[41, 52, 63]

此时就每列元素并行地应用fun方法,同样返回列表

map大概意思就是这样,下面盗一张网络的图,比较形象

map-多个seq

发表评论