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的剔除。

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

返回结果:

  1. [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,那么返回值为序列本身。

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

返回结果:

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

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

  1. [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)。如果初始存在,则将其放在计算序列项之前,当序列为空时作为默认值。

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

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

  1. 21

如果传入初始值得话则:

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

结果为:

  1. 121

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

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

结果为

  1. 3
  2. 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. vue.js 1.0中用v-for遍历出的li中的@click事件在移动端无效

    在vue.js使用v-for遍历出的li中的@click事件在移动端无效,在网页端可以执行,代码如下 <template> <div class="rating-secti ...

  2. Cassandra二级索引原理——新创建了一张表格,同时将原始表格之中的索引字段作为新索引表的Primary Key,并且存储的值为原始数据的Primary Key,然后再通过pk一级索引找到真正的值

    1.什么是二级索引? 我们前面已经介绍过Cassandra之中有各种Key,比如Primary Key, Cluster Key 等等.如果您对这部分概念并不熟悉,可以参考之前的文章: [Cassan ...

  3. 【Hive】数据去重

    实现数据去重有两种方式 :distinct 和 group by 1.distinct消除重复行 distinct支持单列.多列的去重方式. 单列去重的方式简明易懂,即相同值只保留1个. 多列的去重则 ...

  4. js控制iframe的刷新(页面局部刷新)

    今天遇到个问题,后台会员审核之后,页面内的会员审核状态要及时改变,但又不能指望用户手动刷新(用户体验很不好) 如果审核页面和显示审核状态时同在一个html页面的话,那么直接用js改变div内部的文本就 ...

  5. Drools7在Intellij IDEA下的引入静态方法错误提示

    问题 在Intellij IDEA 2016下,默认安装了Drools的插件,但使用Drools7(其他版本应该也有问题)时发现,在DRL文件中引入的静态方法IDEA会提示"Cannot r ...

  6. haroopad 语法高亮问题

    <!DOCTYPE html> Untitled.html div.oembedall-githubrepos{border:1px solid #DDD;border-radius:4p ...

  7. Gradle配置IDEA正常识别JPA Metamodel Generator动态生成的代码

    我们在使用JPA动态查询构建查询条件时,为了实现安全的类型检查,常常需要引用Hibernate JPA Metamodel Generator自动为我们生成静态元模型类. 而这些类由于编译时由Hibe ...

  8. 【Android】SDK工具学习 - adb

    ADB(Android Debug Bridge) 小白笔记 学习资料 adb简要介绍 adb 是一个 C/S 架构的命令行工具,主要由 3 部分组成: 运行在 PC 端的 Client : 可以通过 ...

  9. 【解题报告】[动态规划]-PID69 / 过河卒

    原题地址:http://www.rqnoj.cn/problem/69 解题思路: 用DP[i][j]表示到达(i,j)点的路径数,则 DP[0][0]=1 DP[i][j]=DP[i-1][j]+D ...

  10. HDU - 6103 :Kirinriki(不错的尺取法)

    We define the distance of two strings A and B with same length n is dis A,B =∑ i=0 n−1 |A i −B n−1−i ...