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.

  1. >>> l=[1,2,3,4,5]
  2. >>> f = lambda x: True if x%2 == 0 else False
  3. >>> filter(f,l)
  4. [2, 4]
  5. >>> t=(1,2,3,4,5)
  6. >>> filter(f,t)
  7. (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).

  1. >>> f= lambda y : y*2
  2. >>> l=[1,2,3]
  3. >>> t=(1,2,3)
  4. >>> map(f,l)
  5. [2, 4, 6]
  6. >>> map(f,t)
  7. [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.

  1. >>> l = [1,2,3,4,5,6,7,8,9]
  2. >>> f = lambda x,y : x+y
  3. >>> reduce(f,l)
  4. 45
  5. >>> reduce(f,l, 20)
  6. 65

python: filter, map, reduce, lambda的更多相关文章

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

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

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

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

  3. python filter map reduce

    filter(function, iterable): Construct a list from those elements of iterable for which function retu ...

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

    1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...

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

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

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

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

  7. Python经常使用内置函数介绍【filter,map,reduce,apply,zip】

    Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...

  8. Python内置函数之filter map reduce

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

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

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

随机推荐

  1. java-IO操作性能对比

    在软件系统中,IO速度比内存速度慢,IO读写在很多情况下会是系统的瓶颈. 在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作,以字节为处理单位:Reader ...

  2. Node.js——Stream

    介绍 文件流:我们一般对大一点的文件实现stream的方式进行操作 http:显然http.createServer创建过程中的IncomingMessage实现了可读流的接口,ServerRespo ...

  3. 初学者对C++的切身感受

    上周和一同学聊起了当前一些比较流行且运用广范的编程语言,苹果的IOS比起其它语言 来说更加言简意赅,简单明了,并且他现在也打算一直弄IOS.我之前一直是用C语言和 GNU ARM汇编语言,因为这两种语 ...

  4. jquery 点击切换div

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. vue工程化之去除Eslint验证

    有的时候用vue-cli创建好项目之后,写代码时会出现换行和空格报错,出现这么写错误是什么原因呢? 相信第一次接触时有点摸不着头脑.其实是在你用vue-cli脚手架构建项目时用了ESLint代码检查工 ...

  6. java正则表达式的进阶使用20180912

    package org.jimmy.autosearch20180821.test; import java.util.regex.Matcher; import java.util.regex.Pa ...

  7. 记一次被面试的final问题

    ---- 前言 今天面试被问到了,我们都知道final修饰的东西是不可变的,那么是值不可变还是其地址不可变?一脸懵逼,回来查阅一番,总结一下 --- final与数据 在日常行为下,一般数据指的都是基 ...

  8. CentOS7安装Tomcat9并设置开机启动

    1.下载 Tomcat 9 CentOS 7 下创建目录并下载文件: cd /usr/local/ mkdir tomcat cd tomcat wget http://mirrors.hust.ed ...

  9. Spider-scrapy 中的 xpath 语法与调试

    把setting中的机器人过滤设为False ROBOTSTXT_OBEY = False 1 语法 artcile 选取所有子节点 /article 选取根元素 artile article/a 选 ...

  10. 02--Activiti初始化表

    初始化数据库 方法一:执行sql脚本文件activiti-5.13\database\create\activiti.mysql.create.*.sql文件 方法二:代码创建(有流程自然就有表) p ...