'''
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. python+NLTK 自然语言学习处理二:文本

    在前面讲nltk安装的时候,我们下载了很多的文本.总共有9个文本.那么如何找到这些文本呢: text1: Moby Dick by Herman Melville 1851 text2: Sense ...

  2. webpack前端工程化构建工具的使用

    一.模块打包机 1.创建文件 在目标文件下建立一个src文件夹作为js代码区:作为例子,我创建了两个js文件,并利用commonJS规范require引入到index.js中: moduleA.js: ...

  3. 【原创】Kafka Consumer多线程实例续篇

    在上一篇<Kafka Consumer多线程实例>中我们讨论了KafkaConsumer多线程的两种写法:多KafkaConsumer多线程以及单KafkaConsumer多线程.在第二种 ...

  4. Latex加速: TexStudio的安装和使用

    TexStudio可以加速Latex的编辑和写作,这里简单介绍一下TexStudio的安装,配置和使用.但是有一个重要的前提,TexStudio会使Latex源代码和pdf文件并排显示,比较占用桌面的 ...

  5. 【知识整理】这可能是最好的RxJava 2.x 入门教程(五)

    这可能是最好的RxJava 2.x入门教程系列专栏 文章链接: 这可能是最好的RxJava 2.x 入门教程(一) 这可能是最好的RxJava 2.x 入门教程(二) 这可能是最好的RxJava 2. ...

  6. Hibernate基础知识总结

    Hibernate是JDBC的轻量级的对象封装(encapsulation),它是一个独立的对象持久persistence层框架. hibernate要做的事,就是让对象投影到关系数据库中,然后实施化 ...

  7. 用CSS3实现无限循环的无缝滚动

    有时候在页面的某个模块中,需要无限循环的滚动一些消息.那么如果我们用js实现无缝衔接滚动的思路是什么呢(比如我们这个模块是向上滚动的)? 克隆A一份完全一样的数据B放在原数据A的后面: 使用setIn ...

  8. java基础(8) -集合类-Collecion

    集合类-Collecion Collection接口 常用方法 //添加新元素 boolean add (E element); //返回迭代器 Iterator<E> iterator( ...

  9. NavigationView的头部的事件监听

    现在App的UI设计中Drawerlayout+NavigationView是一个比较常用的设计了,而以前我一般只是在Navigation中的menu(即下部的item中)添加事件监听,而今天碰到一个 ...

  10. 投票系统 & js脚本简单刷票

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