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( ...
随机推荐
- Moco框架jar下载地址
1.打开url:http://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.10.0/ 2.下载最大的jar包,如下图所示:
- java的桥接模式
http://blog.csdn.net/jason0539/article/details/22568865 http://www.cnblogs.com/V1haoge/p/6497919.htm ...
- Elasticsearch的前后台运行与停止(rpm包方式)
对应,这es的下载,需要rpm包. Elasticsearch-2.4.3的下载(图文详解) 建议用root用户 [root@djt002 elasticsearch-2.4.3]$ pwd/usr/ ...
- js能否实现截图,截图之后的图片数据再下载到本地?
https://www.zhihu.com/question/20763177 http://www.cnblogs.com/yanweidie/p/5203943.html
- win10 安装nodejs,报错there is a problem in the windows installer package
今天重装了win10系统,开始安装各种软件,装到node的时候我崩溃了,报错there is a problem in the windows installer package··· 度娘了各种安装 ...
- Ubuntu 12.04 Eclipse设…
Ubuntu 12.04 Eclipse设置(黑色背景解决) 分类: ubuntu2012-11-21 10:47 252人阅读 评论(0) 收藏 举报 eclipseEclipseubuntuUbu ...
- 4.ctf实战题
一道ctf实战题. 先亮出网址: http://fb2ad00f-0a28-4e38-8fff-849d7391e2d0.coding.io 打开连接,看到下面页面.Web题,首先就是扫描(御剑啊还有 ...
- 719D(树形dp)
题目链接:http://codeforces.com/contest/791/problem/D 题意:给出一棵树,每两个点之间的距离为1,一步最多可以走距离 k,问要将任意两个点之间的路径都走一遍, ...
- Hyperledger Fabric java chaincode 中文乱码问题
开发java chaincode过程中遇到一个中文乱码的问题.都是官方的demo,请求的sdk是用java写的,部署的chaincode有两种选择(不考虑node),一种go语言写的chaincode ...
- Count the string (KMP+DP)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...