今天阅读了关于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的更多相关文章

  1. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  2. python 高阶函数 lamdad reduce map

    ## def use_filer(l):## # 过滤偶数# rest = filter(lambda n: n % 2 != 0, l)# return rest## if __name__ == ...

  3. python一个注意的地方

    https://www.zhihu.com/question/25874136 class test: l=[] def init(self): self.l=['1','2','7'] a1=tes ...

  4. Python Map, Filter and Reduce

    所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...

  5. [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE

    map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...

  6. Python【map、reduce、filter】内置函数使用说明(转载)

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  7. 【转】Python 中map、reduce、filter函数

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  8. 转:Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)

    Map函数: 原型:map(function, sequence),作用是将一个列表映射到另一个列表, 使用方法: def f(x): return x**2 l = range(1,10) map( ...

  9. Python里的map、reduce、filter、lambda、列表推导式

    Map函数: 原型:map(function, sequence),作用是将一个列表映射到另一个列表, 使用方法: def f(x): return x**2 l = range(1,10) map( ...

随机推荐

  1. ceph安装对象网关

    1.概述 安装3个网关节点分别是:controller-03.controller-04和controller-05,使用ceph gw自带的Civetweb提供服务,前端使用nginx作为前端代理. ...

  2. JavaScript高级程序设计学习笔记第五章--引用类型

    一.object类型 1.创建object类型的两种方式: 第一种,使用构造函数 var person = new Object();或者是var person={};/与new Object()等价 ...

  3. 原生app与WebApp的区别

    Native App开发Native App开发即我们所称的传统APP开发模式(原生APP开发模式),该开发针对IOS.Android等不同的手机操作系统要采用不同的语言和框架进行开发,该模式通常是由 ...

  4. dialog 设置maxHeight 最大高度

    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Displ ...

  5. nginx是如何处理一个请求的(包含https配置)

    配置https首先要有ssl证书,这个证书目前阿里有免费的,但如果自己做实验,也是可以自签证书,只不过不受信 openssl genrsa -des3 -out server.key 1024     ...

  6. Git 分支管理 创建与合并分支

    分支在实际中有什么用呢? 假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了. 如果等代码全部写完再一次提交, ...

  7. linux文件重命名

    rename 命令用字符串替换的方式批量改变文件名. 语法 rename(参数) 参数 原字符串:将文件名需要替换的字符串: 目标字符串:将文件名中含有的原字符替换成目标字符串: 文件:指定要改变文件 ...

  8. ue4 character 物理测试

    charactor里 CapsuleComponnet Mesh CharacterMovement 3个组件里有有物理开关 目前看Mesh的Simulate Physics+Enable Gravi ...

  9. ue4 delegate event

    官网相关 https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Delegates/index.html wi ...

  10. cf791B(完全图&dfs)

    题目链接:http://codeforces.com/contest/791/problem/B 题意:给出一个无向图,问是否满足若存在边ab, bc则存在边ac: 思路:题意即,对于一个点,其所有子 ...