[python] python 中map、reduce和filter 浅析
来源:互联网 发布:康奈尔大学商学院 知乎 编辑:程序博客网 时间:2024/06/05 12:07
0x00 map()
如果你想先了解下什么是map()和reduce(),可以参考MapReduce:Simplified Data Processing On Large Clusters这篇文章。
map() 和 reduce() 都是python 内置函数
如果你想先了解下什么是map()和reduce(),可以参考MapReduce:Simplified Data Processing On Large Clusters这篇文章。
map() 和 reduce() 都是python 内置函数
可以看到在python文档中对map() 、reduce()的描述:
https://docs.python.org/3.4/library/functions.html#map
Return an iterator that applies function to every item of iterable, yielding 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. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().
可以看到,map()函数接受两个参数,function和iterable , function是函数对象,多数时候是自定义的函数,第二个参数iterable是一个可迭代对象,而map()函数的功能就是将函数function作用在可迭代对象iterable中的每一个元素上,并生成一个新的列表,也就是说,我们也可以对tuple等不可改变对象进行map()操作。现在举个map()的例子,假设我们有一个已经排好序的列表 [1,2,3,4,5,6,7], 现在我需要将他们每一个数都乘10再加1,那么用map()函数来实现就是这样:
>>> a = [1, 3, 4, 7, 9, 18]>>> b = map( lambda x: x*10+1, a)>>> b[11, 31, 41, 71, 91, 181]
0x01 reduce()
关于reduce描述在文档中也有描述,不过在python3.x中,reduce()函数被放置在functools模块中,所以使用前需要导入
from functools import reduce
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence 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 sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.
Roughly equivalent to:
>>> list1 = [1, 2, 3, 4, 5 ]>>> list2 = [34, ]>>> list3 = []>>> def func(x, y):... return x * y...>>> reduce(func, list1)120>>> reduce(func, list1, 6)720>>> reduce(func, list2)34>>> reduce(func, list2, 2)68>>> reduce(func, list3)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: reduce() of empty sequence with no initial value
0x02 filter()
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item))
if function is not None and (item for item in iterable if item) if function is None.
See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.
filter翻译过来是过滤器,和map()类似,filter()函数也接受一个函数和一个可迭代序列,和map()不同的时,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。- [python] python 中map、reduce和filter 浅析
- Python中的map、reduce和filter浅析
- python:map filter reduce
- python filter/map/reduce
- python map reduce filter
- python map reduce filter
- python map reduce filter
- python map,reduce,filter
- python中map,filter和reduce函数区别
- Python 中 map,reduce,和filter 内置函数的方法
- python中map、filter、reduce 函数使用方法
- python中reduce()、map()、filter()的学习
- python中map,reduce,filter,zip
- Python中filter、map、reduce函数
- python:filter、lambda和map、reduce
- Python中的Map, Filter和Reduce
- python filter lambda reduce map
- Python: lambda, map, reduce, filter
- android permission各种
- 接口、抽象类、抽象方法
- android 日历 把别人日历整理了下
- ADF调用WebService服务
- 快速排序
- [python] python 中map、reduce和filter 浅析
- linux下安装Oracle
- Container With Most Water ——解题笔记
- 正序分解整数
- DesignPatterns_Prototype
- 信号量 互斥锁 条件变量的区别
- 程序员是青春饭吗
- Linux系统下安装rz/sz命令及使用说明
- ListView被选中的记录置顶显示