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( ...
随机推荐
- NLB
http://www.cnblogs.com/allegro/archive/2011/02/11/1951171.html
- 集成hibernateDaoSupport实现增删改查
1. package edu.jlu.fuliang.dao.impl; import java.util.List; import org.springframework.orm.hibernate ...
- c++控制台 对齐 域宽
包含在头文件 iomanip 设置对齐: cout<<setiosflags(ios::xxx); xxx内填参数 left左对齐 right右对齐 setiosflags还有其他选项, ...
- jquery中的$.ajax()的源码分析
针对获取到location.href的兼容代码: try { ajaxLocation = location.href; } catch( e ) { // Use the href attribut ...
- malloc,alloc,realloc之间的相似与区别
三个函数的申明分别是: void* realloc(void* ptr, unsigned newsize); void* malloc(unsigned size); void* calloc(si ...
- UVaLive 3530 Martian Mining (简单DP)
题意:给定一个n*m的网格,每个格子里有A矿和B矿数量,A必须由右向左运,B只能从下向上运,中间不能间断,问最大总数量. 析:一个简单DP,dp[i][j] 表示 从 (0, 0) 到 (i, j) ...
- 编译出arm Android环境下的C++可执行文件
要想编译出arm环境的C++可执行文件主要就是利用交叉编译器进行编译.编译过程本身都大同小异. 1.安装交叉编译器,交叉编译器的安装方法大致有下面几种: A.debian/ubuntu 系统可以直接输 ...
- 无监督学习:Deep Auto-encoder(深度自动编码器)
一 Auto-encoder NN Encoder & NN Decoder 要一起训练. 二 Starting from PCA 三 Deep Auto-encoder PCA&De ...
- 笔记-JavaWeb学习之旅19
Redis:redis是一款高性能的NOSQL系列的非关系型数据库 NOSQL: Not Only SQL ,意即"不仅仅是SQL",是一项全新的数据库理念,泛指非关系型数据库 r ...
- jquery中的$(this)和this
在jquery中,存在$(this)和this. 其中常见的是出现在事件处理函数中. 首先先来理解jquery对象. jquery对象其实就是DOM对象的集合. 比如:$('a')[0];------ ...