'''
Python --version :Python 2.7.11
Quote : https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
Add by camel97 2017-04
'''
1.filter()
#filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true. If sequence is a str, unicode or tuple, the result will be of the same type; otherwise, it is always a list. For example, to compute a sequence of numbers divisible by 3 or 5:
#你可以把 filter 当成一个过滤器,用来选择原来 list 中满足特定条件的 value。它有两个参数。第一个参数是一个返回 bool 类型的函数,第二个参数可以是一个 list 。 filter()会返回一个新的 list ,这个新的 list 中的值同时满足这样两个条件。第一,他们属于传给 filter() 的 list 的值。第二,它们作为参数传给 function 函数时,函数会返回 True。
 def f(x):
return x % 3 == 0 or x % 5 == 0
print filter(f, range(2, 25)) ==>[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]

显然,filter() 筛选出了原来的 list ( range(2,25) )中能被 3 整除或者能被 5 整除的数

2.map()

#map(function, sequence) calls function(item) for each of the sequence’s items and returns a list of the return values. For example, to compute some cubes:

#map 函数可以把 list 中的每一个 value 传给函数,并且将每一次函数返回的结果合到一起生成一个新的 list

#它可以被用来这样操作:首先定义一个对某一个参数执行算数运算的函数,然后通过 map 函数来对 list 中的每一个 value 进行函数定义过的算数运算

 print map(lambda x:x*x*x, range(1, 11))

 ==>[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

这个 demo 用来对 list(range(1,11))中的每一个 value 执行 f(x) = x*x*x 操作

#More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another).

#map 函数可以传不止一个 list 。但同时函数也必须执行对不止一个参数的算数运算。list 的个数和函数的参数个数应当是对应的。

 seq = range(8)
print map(lambda x,y : x+y, seq, seq) ==>[0, 2, 4, 6, 8, 10, 12, 14]

这个 demo 显示了如何对两个 list 中对应的 value 实行求和操作

 seq = range(8)
seqcopy = range(7)
print map(lambda x,y : x+y, seq, seqcopy) ==>TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

如图所示,当两个 list 中的 value 个数不相等时, 程序会报一个 TypeError 错误。因为一个 NoneType 不能和任何类型的数据进行运算

3.reduce()

#reduce(function, sequence) returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10:

#reduce() 函数返回一个值而不是一个 list 。首先你需要定义一个需要对两个参数进行算数运算的函数。reduce()函数首先会对 list 中的第一个 value 和第二个 value 进行这个算数运算,这会得到一个 result 。之后它会对这个 result 和 list 中的第三个 value 进行这个算数运算得到一个新的 result 。它会这样一直进行下去直到这个 list 中的所有 value 都参与了运算并且返回最后一次的 result。

 print reduce(lambda x,y : x+y ,range(11))

 ==>55

如图完成了对 range(11) 中的10个数的求和操作

#If there’s only one item in the sequence, its value is returned; if the sequence is empty, an exception is raised.

#如果 list 中只有一个数据,那么 reduce() 将不会进行任何运算而是直接返回这个数。如果 list 为空,reduce() 将会报一个异常 TypeError

 print reduce(lambda x,y : x+y,[1])
==>1 print reduce(lambda x,y : x+y,[])
==>TypeError: reduce() of empty sequence with no initial value

#A third argument can be passed to indicate the starting value. In this case the starting value is returned for an empty sequence, and the function is first applied to the starting value and the first sequence item, then to the result and the next item, and so on.

#reduce() 可以添加第三个参数。这个参数相当于一个初始值。当给了第三个参数之后,对于一个空的 list ,reduce() 会返回这个初始值而不是抛出一个异常(和上一种情况做比较)。当给了第三个参数之后,reduce() 会首先计算这个初始值和第一个 value 的算数运算结果,其余步骤均相同

 print reduce(lambda x,y : x+y , range(11) , 11)
==>66 print reduce(lambda x,y : x+y,[1] , 2)
==>3 print reduce(lambda x,y : x+y , [] , 0)
==>0

注意,虽然我们说这三个函数在 list 中是非常有用的,但是它们并不是只能用在 list 这一种数据类型中。

当然。传入的数据类型需要能够被迭代。

python关于list的三个内置函数filter(), map(), reduce()的更多相关文章

  1. Python 内置函数&filter()&map()&reduce()&sorted()

    常用内置函数 Python 2.x 返回列表,Python 3.x 返回迭代器 在进行筛选或映射时,输出的结果是一个数组,需要list帮助. 如:print(list(map(lambda x:x+1 ...

  2. Python内置函数filter, map, reduce

    filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当 ...

  3. Python【zip-map-filter】三个内置函数

    print("============内置函数:zip===========")l2 = ['a','b','c','e','f','g']l3 = [1,2,3]L4=['A', ...

  4. Python装饰器、生成器、内置函数、json

    这周学习了装饰器和生成器,写下博客,记录一下装饰器和生成器相关的内容. 一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如 ...

  5. Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

    Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...

  6. python之有用的3个内置函数(filter/map/reduce)

    这三个内置函数还是非常有用的,在工作中用的还不少,顺手,下面一一进行介绍 1.filter 语法:filter(function,iterable) 解释:把迭代器通过function函数进行过滤出想 ...

  7. 函数进阶· 第3篇《常用内置函数filter()、map()、zip(),怎么用的呢?》

    坚持原创输出,点击蓝字关注我吧 作者:清菡 博客:oschina.云+社区.知乎等各大平台都有. 由于微信公众号推送改为了信息流的形式,防止走丢,请给加个星标 ,你就可以第一时间接收到本公众号的推送! ...

  8. 内置函数filter()和匿名函数lambda解析

    一.内置函数filter filter()函数是 Python 内置的一个高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回由符合条件迭代器 ...

  9. 内置函数_map()、reduce()、filter()

    map().reduce().filter() map()内置函数把一个函数func依次映射到序列或迭代器对象的每个元素上,并返回一个可迭代的map对象作为结果,map对象中每个元素是原序列中元素经过 ...

随机推荐

  1. Swift json字典转模型 项目记录

    背景 最近项目开始转用Swift3开发,由于Swift中json(字典)转模型的选择方案较多,笔者最开始选择了HandyJSON的方案,在使用一段时间后发现当要进行某个字段取值使用时需要进行各种的转化 ...

  2. Discuz搜索改为指向帖子

    安装的版本是DiscuzX2.5,搜索的时候发现默认指向的是门户里的文章搜索,但程序都没有安装门户,只有论坛,所以不能搜索文章. 在网上找了半天终于找到修改的办法了. <input name=& ...

  3. iOS-Core-Animation-Advanced-Techniques(一)

    视图(UIView)和图层(CALayer)的关系: 每一个UIview都有一个CALayer实例的图层属性,视图的职责就是创建并管理这个图层,以确保当子视图在层级关系中添加或者被移除的时候,他们关联 ...

  4. js调试模式控制台输出信息

    js调试模式控制台输出信息.console.log

  5. tomcat运行war包报错,找不到context-root文件

    今天在部署项目的时候遇到了这个问题,查看Tomcat日志/logs/cataline.out这个文件. 里面有一句:can not open .....[context-root.xml], 进过很长 ...

  6. OpenStack(企业私有云)万里长征第三步——调试成功

    一.前言 折腾了一两个月(中间有事耽搁了半个月),至今日基本调试成功OpenStack,现将中间的部分心得记录下来. 二.环境 使用的是devstack newton版.具体部署过程可以参考cloud ...

  7. zookeeper client API实现(python kazoo 的实现)

    这里主要分析zookeeper client API的实现方式,以python kazoo的实现代码为蓝本进行逻辑分析. 一.代码框架及介绍 API分为同步模式和异步模式.同步模式是在异步模式的基础上 ...

  8. 增广拉格朗日乘子法(Augmented Lagrange Method)

    转载自:增广拉格朗日乘子法(Augmented Lagrange Method) 增广拉格朗日乘子法的作用是用来解决等式约束下的优化问题, 假定需要求解的问题如下: minimize f(X) s.t ...

  9. 【Android Developers Training】 21. 创建一个可变动的UI

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  10. 使用asp.net mvc部分视图渲染html

    为了提升用户体验,一般我们采用ajax加载数据然后根据数据渲染html,渲染html可以使用前端渲染和服务器端渲染. 前端渲染 使用前端模版引擎或MVC框架,例如underscore.js的templ ...