python函数式编程内建函数filter,map,reduce函数

1. filter函数

filter函数会对指定序列执行过滤操作。
filter函数的定义:
filter(function or None, sequence) -> list, tuple, or string
function是一个谓词函数,接受一个参数,返回布尔值True或False。
filter函数会对序列参数sequence中的每个元素调用function函数,最后返回的结果包含调用结果为True的元素。
返回值的类型和参数sequence的类型相同
比如返回序列中的所有偶数:
def is_even(x):
return x & 1 != 0

#随机产生一个序列,筛选出其中的奇数
from random import randint
def odd(x):
    return x%2;

if __name__=='__main__':
    nums=[]
    for x in range(9):
        nums.append(randint(1,100))
    it=filter(odd,nums)
    res=[x for x in it]
    print(res)


from random import randint
nums=[]
for x in range(9):
    nums.append(randint(1,100))
it=filter(lambda x:x%2,nums)
res=[x for x in it]
print(res)


from random import randint as ri
res=[n for n in [ri(1,100) for i in range(9)] if n%2]
print(res)
    >>> def f1(x):  
    if x > 20:  
    return True  
    else:  
    return False  

    >>> l1 = [ 1, 2, 3, 42, 67, 16 ]  
    >>> filter( f1, l1)  
    <filter object at 0x01F594F0>  
    >>> l2 = filter( f1, l1 )  
    >>> print(l2)  
    <filter object at 0x01FA30D0>  
    >>> l2.__next__  
    <method-wrapper '__next__' of filter object at 0x01FA30D0>  
    >>> l2.__next__()  
    42  
    >>> l2.__next__()  
    67  
    >>> l2.__next__()  
    Traceback (most recent call last):  
      File "<pyshell#14>", line 1, in <module>  
        l2.__next__()  
    StopIteration  

2.map函数

map函数会根据提供的函数对指定序列做映射。
map函数的定义:
map(function, sequence[, sequence, …]) -> list
通过定义可以看到,这个函数的第一个参数是一个函数,剩下的参数是一个或多个序列,返回值是一个集合。
function可以理解为是一个一对一或多对一函数,map的作用是以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的list。
比如要对一个序列中的每个元素进行平方运算:
map(lambda x: x ** 2, [1, 2, 3, 4, 5])
返回结果为:
[1, 4, 9, 16, 25]
在参数存在多个序列时,会依次以每个序列中相同位置的元素做参数调用function函数。
比如要对两个序列中的元素依次求和。
map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
map返回的list中第一个元素为,参数序列1的第一个元素加参数序列2中的第一个元素(1 + 2),
list中的第二个元素为,参数序列1中的第二个元素加参数序列2中的第二个元素(3 + 4),
依次类推,最后的返回结果为:
[3, 7, 11, 15, 19]
要注意function函数的参数数量,要和map中提供的集合数量相匹配。
如果集合长度不相等,会以最小长度对所有集合进行截取。
当函数为None时,操作和zip相似:
map(None, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
返回结果为:
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

it=map(lambda x:x+2,[12,3,4,5,66,7])
res=[x for x in it]
print(res)

it1=map(lambda x,y:x+y,[1,3,5],[2,4,6])
res1=[x for x in it1]
print(res1)

it2=map(lambda x,y:(x+y,x-y),[1,3,5],[2,4,6])
res2=[x for x in it2]
print(res2)

it3=map(lambda x,y,z:(x,y,z),[1,3,5],[2,4,6],[7,8,9])
res3=[x for x in it3]
print(res3)
    >>> def f1( x, y ):  
    return (x,y)  

    >>> l1 = [ 0, 1, 2, 3, 4, 5, 6 ]  
    >>> l2 = [ 'Sun', 'M', 'T', 'W', 'T', 'F', 'S' ]   
    >>> l3 = map( f1, l1, l2 )  
    >>> print(list(l3))  
    [(0, 'Sun'), (1, 'M'), (2, 'T'), (3, 'W'), (4, 'T'), (5, 'F'), (6, 'S')]  

    >>> print( list(map(f2, l1) ))  
    [0, 2, 4, 6, 8, 10, 12]  

    >>> print( list(map(f2, l2) ) )  
    ['SunSun', 'MM', 'TT', 'WW', 'TT', 'FF', 'SS']  

    >>> def f3( x, y ):  
    return x*2, y*2  

    >>> print( list(map(f3, l1, l2) ))  
    [(0, 'SunSun'), (2, 'MM'), (4, 'TT'), (6, 'WW'), (8, 'TT'), (10, 'FF'), (12, 'SS')]  

3.reduce函数

python3.4中reduce从全局模块中移到了functools中
reduce函数,reduce函数会对参数序列中元素进行累积。
reduce函数的定义:
reduce(function, sequence[, initial]) -> value
function参数是一个有两个参数的函数,reduce依次从sequence中取一个元素,和上一次调用function的结果做参数再次调用function。
第一次调用function时,如果提供initial参数,会以sequence中的第一个元素和initial作为参数调用function,否则会以序列sequence中的前两个元素做参数调用function。
reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1)
结果为21( (((((1+2)+3)+4)+5)+6) )
reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])
结果为20

from functools import reduce
res=reduce((lambda x,y:x+y),[1,2,3,4],10)
print(res)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值