'''
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. SQL Server 2012 酸爽的安装体验

    电脑上已经安装了SQL Server 2008 R2,要想安装SQL Server 2012,必须先将已安装的SQL Server 2008 R2 安全卸载,否则安装过程中会报错! 使用到的卸载软件有 ...

  2. 【LeetCode】220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. Python如何调用新浪api接口的问题

    前言:这些天在研究如何调用新浪开放平台的api分析新浪微博用户的数据 成果:成功调用了新浪api获取了用户的一些个人信息和无数条公共微博 不足:新浪开放平台访问有限制,返回的数据着实有限,不足以分析问 ...

  4. JS实现全选、不选、反选

    思路:1.获取元素.2.用for循环历遍数组,把checkbox的checked设置为true即实现全选,把checkbox的checked设置为false即实现不选.3.通过if判断,如果check ...

  5. 不借助第三方网站四步实现手机网站转安卓APP

    今天本来是帮朋友查看是否在APP里可以点外链的一个测试,做着做来感觉了,就把这个测试优化了一下.好了我们来进入正题. 工具:Android Studio 第一步:新建项目 第二步:拖入控件(WebVi ...

  6. angularJS 源码阅读之一:toDebugString

    简介: 这个函数返回调试字符串: number,boolean,string,null,undefined,都会转为字符串. function 中括号前面有空格的,会去除函数体,没空格的,会输出函数的 ...

  7. Python3中的模块

    模块使用哪种语言实现并不重要,因为所有的模块导入与使用的方式都相同. 1.常用模块导入格式: import importable1,importable2,... import importable ...

  8. iconfont字体图标的使用方法--超简单!

    我之前因为项目用bootstrap比较多,所以使用font awesome字体图标比较多,后来接触到了iconfont,发现想要的什么图标都有,还可以自定义图标,非常强大!之前看了一波教程,觉得繁琐, ...

  9. Wince 创新布局

    如果你的项目是用wince开发并且机器是小型的pda,你可以考虑有这种布局方式. IDE上布局,如图 /// <summary> /// 显示层 /// </summary> ...

  10. 十年过去了,各位 .net 兄弟还好吗

    时间是最无情的,一下子就毕业10年了.很久没有发发牢骚了,今天突然想发一下.看过我文章喷过的知道,我一般都是散文,看完不知道我写了什么,形散而神不散嘛. 十年了,不好意思,没像网上说的标准一样,做管理 ...