python: filter, map, reduce, lambda
filter built-in function
filter(f,sequence)
filter can apply the function f to each element of sequence. If return is true the element will be returned and re-organized to be a new sequence. The type of new sequence can be list/tuple/string, this depends on the input sequence type.
>>> l=[1,2,3,4,5]
>>> f = lambda x: True if x%2 == 0 else False
>>> filter(f,l)
[2, 4]
>>> t=(1,2,3,4,5)
>>> filter(f,t)
(2, 4)
map built-in function
map(f, sequence)
map will apply f to each element of sequence. The result will be returned to be a new list(Unless filter, the return will always a list).
>>> f= lambda y : y*2
>>> l=[1,2,3]
>>> t=(1,2,3)
>>> map(f,l)
[2, 4, 6]
>>> map(f,t)
[2, 4, 6]
reduce() built-in function
reduce(f, sequence, start_value)
reduce apply f to sequence by the order of the sequence. If there is a start_value, this one will be called first.
>>> l = [1,2,3,4,5,6,7,8,9]
>>> f = lambda x,y : x+y
>>> reduce(f,l)
45
>>> reduce(f,l, 20)
65
python: filter, map, reduce, lambda的更多相关文章
- filter,map,reduce,lambda(python3)
1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- python filter map reduce
filter(function, iterable): Construct a list from those elements of iterable for which function retu ...
- Python中特殊函数和表达式 filter,map,reduce,lambda
1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
随机推荐
- ScrollView嵌套GridView,GridView显示不全
最近开发有个需求是以A-Z的方式区分全国城市(搜索功能),并实现字母索引的功能(有点类似微信,不过比较坑的是用的是GridView, 并且GridView上面还有几个LinearLayout). 详细 ...
- /etc/auto.master - automounter的主映射文件
描述(DESCRIPTION) 当机器启动自动挂载器时, autofs(8) 脚本就会查寻 auto.master 这个主映射文件.文件中的每行分别指明,一个挂载点以及与对应的需要被挂载的文件系统.通 ...
- 谈谈JVM垃圾回收机制及垃圾回收算法
一.垃圾回收机制的意义 Java语言中一个显著的特点就是引入了垃圾回收机制,使c++程序员最头疼的内存管理的问题迎刃而解,它使得Java程序员在编写程序的时候不再需要考虑内存管理.由于有个垃圾回收机制 ...
- 【东软实训】SQL多表链接
如果一个查询同时涉及两个以上的表,则称之为链接查询,链接查询是关系数据库中最主要的查询,主要包括等值链接查询.非等值链接查询.自身链接查询.外链接查询和复合条件链接查询. 这篇博文我们来对多表链接进行 ...
- asp.net C# 获得配置文件AppSettings 的值
using System.Configuration;//导入命名空间 //配置文件 Web.config <appSettings> <!--数据连接字符串--> <a ...
- 【原】jq简易教程
https://www.jianshu.com/p/3522fe70de19 https://www.jianshu.com/p/6de3cfdbdb0e 1. jq简介 jq可以对json数据进行分 ...
- 第3节 mapreduce高级:12、mapreduce相关的参数调整
5.1 多job串联 一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: ControlledJob ...
- js 实现弹力球效果
1.html代码: <div id='imgid'> <img src="img/5.png"> </div> 2.js代码: imgobj=d ...
- 笔试算法题(53):四种基本排序方法的性能特征(Selection,Insertion,Bubble,Shell)
四种基本算法概述: 基本排序:选择,插入,冒泡,希尔.上述算法适用于小规模文件和特殊文件的排序,并不适合大规模随机排序的文件.前三种算法的执行时间与N2成正比,希尔算法的执行时间与N3/2(或更快)成 ...
- java1.8学习-什么样的匿名内部类能被lambda语法代替?
java1.8学习-什么样的匿名内部类能被lambda语法代替? java1.8好多新的特性真的很有意思,特别是Lambda.在学习的时候发现并不是所有的匿名内部类都可以用Lambda代替. lamb ...