python关于list的三个内置函数filter(), map(), reduce()

''' 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。
1 def f(x):
2     return x % 3 == 0 or x % 5 == 0
3 print filter(f, range(2, 25))
4 
5 ==>[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 进行函数定义过的算数运算

1 print map(lambda x:x*x*x, range(1, 11))
2 
3 ==>[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 的个数和函数的参数个数应当是对应的。

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

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

 

1 seq = range(8)
2 seqcopy = range(7)
3 print map(lambda x,y : x+y, seq, seqcopy)
4 
5 ==>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。

1 print reduce(lambda x,y : x+y ,range(11))
2 
3 ==>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

1 print reduce(lambda x,y : x+y,[1])
2 ==>1
3 
4 print reduce(lambda x,y : x+y,[])
5 ==>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 的算数运算结果,其余步骤均相同

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

 

 

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

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

转载于:https://www.cnblogs.com/camel97/p/6715455.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值