filter、map、reduce区别
1.filter
filter(function,sequence)-->list,tuple or string
1) 参数func是自定义的过滤函数,在函数func(item)中定义过滤规则。果然func为“None”,则过滤项Item都为真,返回所有序列的元素。
(2) 参数sequence为待处理的序列。
(3) filter()函数的返回值由func()的返回值组成的序列,返回的类型与参数sequence的类型相同。
filter 对序列做过滤处理,对自定义的函数参数返回一个为TRUE的结果集,返回的类型与参数sequence的类型相同
def func(x):
if x>0:
return x
print filter(func,[-1,-2,3])) 输出:
【3】
注意:filter()的过滤函数func()参数不能为空
2.reduce
reduce(function,sequence)-->value
对序列中的元素连续操作可以通过循环来处理(如对某个序列中的元素进行累加操作)
(1) 参数func是自定义的函数,实现对参数sequence的连续操作。
(2) 参数sequence为待处理的序列
(3) 参数initial可以省略,如果initial不为空,则initial的值将首先传入func()进行计算。如果sequence为空,则对initial的值进行计算。
def func(x,y):
return x+y
print reduce(func,range(10),10)
print reduce(func,range(3,10)) 输出:
55
42
3.map函数
map(function, sequence) :对sequence中的item依次执行function(item),见执行结果组成一个List返回:
>>> def cube(x): return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> def cube(x) : return x + x
...
>>> map(cube , "abcde")
['aa', 'bb', 'cc', 'dd', 'ee']
另外map也支持多个sequence,这就要求function也支持相应数量的参数输入:
>>> def add(x, y): return x+y
>>> map(add, range(8), range(8))
[0, 2, 4, 6, 8, 10, 12, 14]
eg:
求阶层
s=[]
a=0
for i in range(1,5):
a+=reduce(lambda x,y:x*y,map(lambda x:x+1,range(i)))
s.append(a)
print a,s
filter、map、reduce区别的更多相关文章
- filter,map,reduce,lambda(python3)
1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...
- paip.提升效率---filter map reduce 的java 函数式编程实现
#paip.提升效率---filter map reduce 的java 函数式编程实现 ======================================================= ...
- 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学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...
- 高阶函数 filter map reduce
const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- lambda,filter,map,reduce
# lambda,filter,map,reduce from functools import reduce print('返回一个迭代器') print((x) for x in range(5) ...
随机推荐
- matlab点云处理函数
1. pcread: 输入文件名,返回pointCloud类(用于存储点云).eg: pcloud = pcread(“filename.ply”) 2. pcshow: 输入pointCloud类, ...
- MySQL查询in操作 查询结果按in集合顺序显示
偶尔看到的...或许有人会注意过,但我以前真不知道 SQL: select * from table where id IN (3,6,9,1,2,5,8,7); 这样的情况取出来后,其实,id还是按 ...
- html怎样可是使文本框内容不可修改
html怎样可是使文本框内容不可修改 <input type="text" readonly="readonly" onfocus="alert ...
- spring与mybati整合方法
(1)spring配置文件: <!-- 引入jdbc配置文件 --> <context:property-placeholder location="jdbc.proper ...
- Idea_学习_05_Intellij Idea自动添加注释的方法
二.参考资料 1. Intellij Idea自动添加注释的方法
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- codeforces 651B B. Beautiful Paintings
B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...
- codeforces 651A A. Joysticks (模拟)
A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- FFmpeg内存操作(三)内存转码器
相关博客列表 : FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析 FFMPEG内存操作(二)从内存中读取数及数据格式的转换 FFmpeg内存操作(三)内存转码器 本文 ...