【python】filter,map,reduce和lambda函数介绍
filter(function, iterable)map(function, iterable)reduce(function, sequence) filter将 function依次作用于iterable的每个元素,如果返回值为true, 保留元素,否则从iterable里面删除。function必须返回是一个bool类型的函数。例如:
def test(x):
return (x > 3)
filter(test, [1, 2, 3, 4, 5]) =====> [4, 5]
map将function作用于iterable每个元素,将对应输出结果保存为一个list。function具有返回值。
例如
def add(x):
return (1 + x)
map(test, [1, 2, 3, 4, 5]) =====> [2, 3, 4, 5, 6]
reduce先把iterable的前两个元素调用函数 function,再以返回值和第三个参数调用,依次执行下去
def add(x,y): return x+y reduce(add, range(1, 11)) 55
lambda:这是Python支持一种有趣的语法,它允许你快速定义单行的最小函数,类似与C语言中的宏,这些叫做lambda的函数,是从LISP借用来的,可以用在任何需要函数的地方:
>>> g = lambda x: x * 2 >>> g(3) 6 >>> (lambda x: x * 2)(3) 6
func = lambda x,y:x+y
def func(x,y):
return x+y
[(lambda x:x*x)(x) for x in range(1,11)]
【python】filter,map,reduce和lambda函数介绍的更多相关文章
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- python filter map reduce
filter(function, iterable): Construct a list from those elements of iterable for which function retu ...
- python之map、filter、reduce、lambda函数
map map函数根据提供的函数对指定的序列做映射,定义:map(function, sequence[,sequence,...])--->list 例1 >>> map(l ...
- python: filter, map, reduce, lambda
filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...
- Python使用map,reduce高阶函数模拟实现Spark的reduceByKey算子功能
# 使用默认的高阶函数map和reduce import randomdef map_function(arg): # 生成测试数据 return (arg,1) list_map = list(m ...
- 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是 ...
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
随机推荐
- 学习iOS的一些网站收藏
1,CocoaChina:http://www.cocoachina.com/ 2,Code4App:http://code4app.com/ 3,梦维:http://www.dreamingwish ...
- ZOJ 1090 The Circumference of the Circle
原题链接 题目大意:已知三角形的三个顶点坐标,求其外接圆的周长. 解法:刚看到这道题时,马上拿出草稿纸画图,想推导出重心坐标,然后求出半径,再求周长.可是这个过程太复杂了,写到一半就没有兴致了,还是求 ...
- 获取验证码,60秒倒计时js
<input type="button" id="btn" value="免费获取验证码" /><script type= ...
- Js的引用赋值与传值赋值
要说js的赋值方式时首先要说明js的数值类型:基本类型和引用类型. 1.基本类型 基本的数据类型有:undefined,boolean,number,string,null. 基本类型存放在栈区,访问 ...
- Hibernate控制insert\update语句
- spark新能优化之shuffle新能调优
shuffle调优参数 new SparkConf().set("spark.shuffle.consolidateFiles", "true") spark. ...
- URAL 1076 Trash Trash(最大权匹配)
Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...
- [转] JAVA网站高并发解决方案
http://blog.csdn.net/herrapfel/article/details/9630911
- Android.mk 文件语法详解 转:http://blog.sina.com.cn/s/blog_602f8770010148ce.html
0. Android.mk简介: Android.mk文件用来告知NDK Build 系统关于Source的信息. Android.mk将是GNU Makefile的一部分,且将被Build Syst ...
- MAVEN build ,GOAL plugin ,execution
http://www.avajava.com/tutorials/lessons/what-are-the-phases-of-the-maven-clean-lifecycle.html https ...