'''
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. 通过bin-log对mysql进行数据恢复

    mysqlbinlog --database=数据库名 --start-date="2017-06-01 5:00:00"  --stop-date="2017-06-1 ...

  2. 各开放平台API接口通用SDK序列文章 前言

    最近两年一直在做API接口相关的工作,在平时工作中以及网上看到很多刚接触API接口调用的新人一开始会感到很不适应,要看的文档一大堆,自己要调用的接口找不着,或都找着了不知道怎么去调用,记得包括自己刚开 ...

  3. SCI论文写作中的注意事项

    SCI论文一般都是英文的格式,其中有很多原则和细节需要我们注意,在我完成第一篇SCI论文的过程中,做些记录,同时和大家分享一下这些经验.同时也稍微改变一下园子里的人口比例,都是攻城狮,程序猿什么的也过 ...

  4. 解决VMware虚拟机不能上网的问题

    这是其中一种情况,因为使用360等加速的时候把VMware 的DHCP等服务关闭了,重新启动服务即可,方法是WIN+R 输入cmd,在doc输入services.ms打开服务,找到 把这些都启动即可

  5. 简单RPC框架-业务线程池

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. HybridApp Exception

    HybridApp Exception [创建安卓虚拟机失败]CPU acceleration status:HAXM must be updated(version 1.1.1<6.0.1) ...

  7. 处理input标签的border-radius

    给input设置border-radius效果时一定要先设置border属性,否则会出现左上部有阴影的效果.

  8. win32SDK的hello,world程序

    首次用Code::Blocks写Win32GUI程序,关于GDI+的引用摸索了半天.SDK写GUI比较累人,以后还是考虑Qt或者其他方式. 代码: /** *code by lichmama from ...

  9. Hibernate 集合映射 一对多多对一 inverse属性 + cascade级联属性 多对多 一对一 关系映射

    1 . 集合映射 需求:购物商城,用户有多个地址. // javabean设计 // javabean设计 public class User { private int userId; privat ...

  10. JavaWeb 后端 <六> 之 EL & JSTL 学习笔记

    一.EL表达式(特别重要)