Python内置函数map与reduce用法简介
来源:互联网 发布:孙侨潞淘宝店叫什么 编辑:程序博客网 时间:2024/05/22 14:08
map与reduce是两个十分常用的Python内置函数,它们与Hadoop中的MapReduce在某些方面有一定的相似之处。
map函数:
map(function, iterable, ...)
对于可迭代对象(iterable)中的每一个元素调用处理函数(function),并以列表(list)形式返回每个元素的调用结果。如果传递了不止一个可迭代对象参数,函数从各个可迭代对象中取出相同位置的元素加以并行处理。如果可迭代对象长短不一,则为较短的参数末尾补充None元素,使其长度补齐。如果处理函数为None,则视为恒等函数(identity function,返回值等于传入参数的函数叫做恒等函数);如果传入了多个参数,map()会返回一个包含各个可迭代对象对应结果的元组列表(可以视为转置操作)。可迭代参数可以是一个序列(sequence),或者任何可以迭代的对象;结果总是返回list。
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.
map函数示例:
基本用法:
>>> map(lambda x : x*2, [1, 2, 3, 4, 5])[2, 4, 6, 8, 10]
传递多个iterable对象:
>>> map(lambda x, y : x+y, [1, 3, 5], [2, 4, 6])[3, 7, 11]
传递多个长短不一的iterable对象:
>>> map(lambda x, y : str(x)+str(y), ['a', 'e', 'i'], ['o', 'u'])['ao', 'eu', 'iNone']
function为None:
>>> map(None, [1, 2, 3, 4, 5])[1, 2, 3, 4, 5]
function为None,且传递多个iterable对象:
>>> map(None, [1, 3, 5], [2, 4, 6])[(1, 2), (3, 4), (5, 6)]
reduce函数:
reduce(function, iterable[, initializer])
对可迭代对象(iterable)从左向右累积地调用处理函数(function),从而将可迭代对象缩减为单个值。例如,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 等价于计算 ((((1+2)+3)+4)+5)。 左边的参数x是被累积的值, 右边的参数y是从迭代对象更新的值。如果传入了可选的初始值(initializer),它会置于迭代对象中的元素参与运算之前,并且在可迭代对象为空时充当缺省值。如果没有提供初始值,并且可迭代对象只包含单个元素时,返回第一个元素。大致等价于下面的代码:
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:
def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: try: initializer = next(it) except StopIteration: raise TypeError('reduce() of empty sequence with no initial value') accum_value = initializer for x in it: accum_value = function(accum_value, x) return accum_value
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
reduce函数示例:
基本用法:
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])15
传入初始值:
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5], 6)21
传入初始值,iterable对象为空列表:
>>> reduce(lambda x, y: x+y, [], 6)6
传入只包含一个元素的iterable对象:
>>> reduce(lambda x, y: x+y, [1])1
当初始值与可迭代对象同时为空时,会抛出下面的错误提示:
Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: reduce() of empty sequence with no initial value
- Python内置函数map与reduce用法简介
- python内置函数map/reduce/filter
- python内置函数map/reduce/filter
- python map ,reduce filter内置函数使用说明
- python 内置函数map/filter/reduce
- python 内置函数 map、 reduce 、filter
- python 内置函数filter, lambda, map, reduce
- python 内置函数map、reduce的使用
- Python 内置函数 lambda、filter、map、reduce
- python内置函数map/reduce/filter
- Python【map、reduce、filter】内置函数使用说明
- python内置函数lambda、filter、map、reduce
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- 【Python】map、filter、reduce函数简介
- map,filter,reduce三个内置函数的用法备忘
- map、reduce、filter内置函数
- python几个内置函数之-filter,map,reduce
- codeforceB. Mishka and trip
- Android studio如何查看Log文件夹
- css鼠标手型cursor属性
- 第七周--项目5排队看病模拟
- spring-定时
- Python内置函数map与reduce用法简介
- 第8周 项目5 计数的模式匹配
- 【并行计算-CUDA开发】__syncthreads的理解
- [感想]读《为什么你总当冤大头?》有感
- 内部类
- 背包问题求解
- 第七周项目1- 建立顺序环形队列算法库
- std::copy 详解
- WebSphere8.5的安装