1. filter

官方解释:filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.

传入参数:function或None,序列

功能:将按照function中定义的方式进行过滤,返回True的留下,返回False的剔除。

 #coding=utf-8
def findPositiveNum(num):
if num > 0:
return True
else:
return False list={2,1,3,-2,0,12,5,-9}
print filter(findPositiveNum,list)

返回结果:

[1, 2, 3, 5, 12]

  

2. map

官方解释:

map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given,  the function is called with an argument list consisting of the  corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return  a list of the items of the sequence (or a list of tuples if more than one sequence).

传入参数:function或None,序列

功能:将序列的中的各个参数传入function中作为参数执行,返回的结果就是序列的值传入function中执行的结果,是一个序列。如果function为None,那么返回值为序列本身。

 def multiValue(num):
return num**2 list1={1,3,5,7,9,11}
print map(multiValue,list1)

返回结果:

[1, 9, 25, 49, 81, 121]

如果function传入为None则返回值为

[1, 3, 5, 7, 9, 11]

3. reduce

官方解释:

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,from left to right, so as to reduce the sequence to a single value.For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

将两个参数的函数累积到序列项上,从左到右,以将序列减少到单个的子序列示例中,减少(λx,y:x+y,[1,2,3,4,5])计算(1+2)+3)+4)。如果初始存在,则将其放在计算序列项之前,当序列为空时作为默认值。

 def addNum(num1,num2):
return num1+num2 list2={1,2,3,4,5,6}
print reduce(addNum, list2)

代码解释:先计算1+2的值,然后将计算的值作为第一个参数传入addNum中,第二个参数为3,依次类推。 最终结果为:

21

如果传入初始值得话则:

 def addNum(num1,num2):
return num1+num2 list2={1,2,3,4,5,6}
#print reduce(addNum, list2)
print reduce(addNum, list2,100)

结果为:

121

4. lambda  可以用来定义匿名函数

 addnum= lambda x,y:x+y
print addnum(1,2)
multiNum=lambda x:x**2
print multiNum(5)

结果为

3
25

Python中特殊函数和表达式 filter,map,reduce,lambda的更多相关文章

  1. Python内置函数之filter map reduce

    Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...

  2. filter,map,reduce,lambda(python3)

    1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...

  3. Python2.7学习笔记-定义函数、filter/map/reduce/lambda

    我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...

  4. Python之匿名函数(filter,map,reduce)

    参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...

  5. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  6. Python中特殊函数和表达式lambda,filter,map,reduce

    1.lambda:使用lambda表达式可以定义一个匿名函数 lambda表达式是一种简洁格式的函数.该表达式不是正常的函数结构,而是属于表达式的类型 (1)基本格式: lambda 参数,参数... ...

  7. Python中 filter | map | reduce | lambda的用法

      1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...

  8. python: filter, map, reduce, lambda

    filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...

  9. Python学习(五)函数 —— 内置函数 lambda filter map reduce

    Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...

随机推荐

  1. shell编程学习1

    1.shell是操作系统的终端命令行 (1)shell可以理解为软件系统提供给用户操作的命令行界面,可以说它是人机交互的一种方式.    (2)我们可以使用shell和操作系统.uboot等软件系统进 ...

  2. vue如何循环同一个echarts图表

    因为我们知道echarts图表需要一个ID节点,所以我们循环echarts同一个图表时要考虑ID节点变化问题.废话不多说,直接上demo效果. 这里有一位分析师在不同的模拟组合,这时需求要在dialo ...

  3. 什么是Activity,详细介绍Activity

    首先,Activity是Android系统中的四大组件之一,可以用于显示View.Activity是一个与用记交互的系统模块,几乎所有的Activity都是和用户进行交互的,但是如果这样就能说Acti ...

  4. 通过TortoiseSVN checkout的文件前面没有“状态标识”

    问题描述:安装完成VisualSVN Server.VisualSVn和TortoiseSVN后,然后通过SVN Server新建Repository(仓库),用Visual Studio新建一个So ...

  5. Android下利用RadioGroup和RadioButton实现Tabbar的效果

    本实现方法主要使用RadioGroup和RadioButton的组合方式来实现Tabbar的效果. 其中选中的Tab的切换的动作可以通过RadioGroup的OnCheckedChangeListen ...

  6. R语言:数据的分割-计算-整合(split-apply-aggregate)

    当获取到原始数据时,我们通常的做法是对该数据进行分割成小片段,然后对各小片段进行计算统计,最后整合成最终的数据.这是统计学里数据处理的一般规律. R语言为我们提供了相应的函数来分别处理这三个阶段任务. ...

  7. CSS3盒模型display:box简述

    display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布局应用就是布局的垂直等高.水平均分.按比例划分. box-f ...

  8. 部署tinyproxy透明代理服务

    线上需要一个https的透明代理,开始打算用nginx,调试了一段时间发现配置较复杂且没有成功.后来用的tinyproxy做的透明代理.安装配置过程就是下载.解压.编译.安装.配置.启动一波流: 安装 ...

  9. css3单冒号和双冒号的区别

    css3中对于伪元素的使用,在项目开发中使用得当将会对代码的可读性又很大的提升.但是对于伪类大家或许都知道是一些选择器的使用,这里总结了关于伪元素中单冒号和双冒号的区别: 再官方定义中规定单冒号都为伪 ...

  10. CentOS下安装w3m,及w3m的使用

    centos下安装软件的命令不是apt-get,而是yum,如果安装w3m,利用sudo yum install w3m w3m-img -y即可 △△△△△△△△如果你用的是centos或readh ...