Python一个有意思的地方:reduce、map、filter
今天阅读了关于Python函数式编程的系列文章,地址在这里:
http://www.cnblogs.com/huxi/archive/2011/06/24/2089358.html
里面提到了四个内建迭代函数:reduce、map、filter、zip。其中zip是供同时迭代多个迭代器用的,这里就不讨论了。主要讨论剩下的三个。
我发现一个有意思的事情,就是剩下的三个函数,reduce、map和filter,三者可以相互转换。例如以reduce为基础,可以实现map和filter函数如下:
def _map(func, iterable):
return reduce(lambda lst, x: lst.append(func(x)) or lst, iterable, []) def _filter(func, iterable):
return reduce(lambda lst, x: lst.append(x) or lst if func(x) else lst, iterable, [])
上面的or操作符是用作流程控制的, lst.append(x) or lst 会将x添加到lst中去, 然后返回lst,因为lst.append(x)会返回None。
基于map或filter去实现其他的函数也是可以的,只不过它们都不像基于reduce实现的map和filter那样简洁。贴出实现如下:
这个是基于map去实现reduce和filter:
#map as the base def _reduce(func, iterable, init):
result = init
map(lambda x: result = func(result, x), iterable)
return result def _filter(func, iterable):
lst= []
map(lambda x: lst.append(x) if func(x), iterable)
return lst
这个是基于filter去实现另外两者:
#filter as the base def _reduce(func, iterable, init):
result = init
filter(lambda x: result = func(result, x), iterable)
return result def _map(func, iterable):
lst = []
filter(lambda x: lst.append(func(x)), iterable)
return lst
可以发现它们大同小异,不是很有意思。
Python一个有意思的地方:reduce、map、filter的更多相关文章
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- python 高阶函数 lamdad reduce map
## def use_filer(l):## # 过滤偶数# rest = filter(lambda n: n % 2 != 0, l)# return rest## if __name__ == ...
- python一个注意的地方
https://www.zhihu.com/question/25874136 class test: l=[] def init(self): self.l=['1','2','7'] a1=tes ...
- Python Map, Filter and Reduce
所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
- Python【map、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 转:Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
Map函数: 原型:map(function, sequence),作用是将一个列表映射到另一个列表, 使用方法: def f(x): return x**2 l = range(1,10) map( ...
- Python里的map、reduce、filter、lambda、列表推导式
Map函数: 原型:map(function, sequence),作用是将一个列表映射到另一个列表, 使用方法: def f(x): return x**2 l = range(1,10) map( ...
随机推荐
- 《精通Spring4.X企业应用开发实战》读后感第五章(Bean作用域)
- NOIP2014提高组 联合权值(距离为2的树形dp)
联合权值 题目描述 无向连通图 GG 有 nn 个点,n-1n−1 条边.点从 11 到 nn 依次编号,编号为 ii 的点的权值为 W_iWi,每条边的长度均为 11.图上两点 (u, v)(u, ...
- 一、初识mybatis
orm框架 1.配置文件(配置别名.mapper xml文件.数据库连接.事务) 2.创建SqlSessionFactory,创建SqlSession 3.创建model,创建Mapper xml文件 ...
- Jinkins定时任务设置
设置的地方在构建触发器中 Build after other projects are built:在其他项目构建完成后再进行构建. Build periodically:周期进行构建 Build w ...
- SmartSql使用教程(2)——使用动态代理实现CURD
一.引言 接着上一篇的教程,本章我们继续讲SmartSql.今天的主题是动态仓储. 老规矩,先上一个项目结构 从第二章开始.我们将原来的单一项目做了一个分离.方便之后的更新. 在这个结构中.原本上一章 ...
- 前端需要了解的http知识
一.五层协议1. OSI(Open System Interconnection 开放式系统互联)七层协议 1)应用层 2)表示层 3)会话层 4)传输层 5)网络层 6)数据链接层 7)物理层2. ...
- codeforces986F Oppa Funcan Style Remastered【线性筛+最短路】
容易看出是用质因数凑n 首先01个因数的情况可以特判,2个的情况就是ap1+bp2=n,b=n/p2(mod p1),这里的b是最小的特解,求出来看bp2<=n则有解,否则无解 然后剩下的情况最 ...
- 输入apt-get update时出现Could not open lock file /var/lib/apt/lists/lock - open
我看了其它的资料发现不够清楚 我只报这些错误 1.1.ps-aux 查出apt-get进程的PID,通常是一个四位数字. 不好找apt-get进程 输入此代码就好找了 ps -aux|grep apt ...
- TensorBoard计算加速
目录 TensorBoard计算加速 0. 写在前面 1. TensorFlow使用GPU 2. 深度学习训练并行模式 3. 多GPU并行 4. 分布式TensorFlow 4.1 分布式Tensor ...
- CreateJS介绍-了解CreateJS
1.CreateJS 一款HTML5游戏开发引擎 CreateJS 是一套可以构建丰富交互体验的 HTML5 游戏的开源工具包,旨在降低 HTML5 项目的开发难度和成本,让开发者以熟悉的方式打造更具 ...