python内置的几个高阶函数:map() ,reduce(),filter()

时间:2022-06-22 19:56:06

map函数

map()函数,map映射  

map(func,iterable)

map()函数接受两个参数,一个是函数,一个可迭代的对象(iterable),map将传入的函数依次作用到序列的每个元素,并把结果作为新的 可迭代的对象 的结果返回

num_l=[1,2,10,5,3,7]
lambda x:x+1
def add_one(x):
    return x+1
def map_test(func,array):
    ret=[]
    for i in num_l:
        res=func(i) #add_one(i)
        ret.append(res)
    return ret
print(map_test(add_one,num_l))
print(map_test(lambda x:x+1,num_l))

输出

[2, 3, 11, 6, 4, 8]
[2, 3, 11, 6, 4, 8]
num_l=[1,2,10,5,3,7]
lambda x:x-1
def reduce_one(x):
    return x-1def map_test(func,array):
    ret=[]
    for i in num_l:
        res=func(i) #add_one(i)
        ret.append(res)
    return ret
print(map_test(reduce_one,num_l))
print(map_test(lambda x:x-1,num_l))

输出

[0, 1, 9, 4, 2, 6]
[0, 1, 9, 4, 2, 6]

map函数

def map_test(func,array): #func=lambda x:x+1    arrary=[1,2,10,5,3,7]
    ret=[]
    for i in array:
        res=func(i) #add_one(i)
        ret.append(res)
    return ret

print(map_test(lambda x:x+1,num_l))
res=map(lambda x:x+1,num_l)  一个可迭代的对象(iterable),只可迭代一次 print('内置函数map,处理结果',res)
for i in res:
    print(i)
print(list(res))
print('传的是有名函数',list(map(reduce_one,num_l)))


msg='liushui'
print(list(map(lambda x:x.upper(),msg)))

输出

[2, 3, 11, 6, 4, 8]
内置函数map,处理结果 <map object at 0x0000000001E9C0F0>
2
3
11
6
4
8
[]
传的是有名函数 [0, 1, 9, 4, 2, 6]
['L', 'I', 'U', 'S', 'H', 'U', 'I']

 filter函数 

作用:对于序列中的元素进行筛选,最终获取到符合条件的序列

演示引出filter

python内置的几个高阶函数:map() ,reduce(),filter()

 

ps1:  过滤掉sb开头的人,就加入到列表中

movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao']
def filter_test(array):
    ret=[]
    for p in array:
        if not p.startswith('sb'):
               ret.append(p)
    return ret

res=filter_test(movie_people)
print(res)

输出

['linhaifeng']

ps2:

movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
def sb_show(n):
    return n.endswith('sb')
def filter_test(func,array):
    ret=[]
    for p in array:
        if not func(p):
               ret.append(p)
    return ret

res=filter_test(sb_show,movie_people)
print(res)

输出

['linhaifeng']

ps3

#终极版本
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
def sb_show(n):
    return n.endswith('sb')
# --->lambda n:n.endswith('sb')

def filter_test(func,array):
    ret=[]
    for p in array:
        if not func(p):
               ret.append(p)
    return ret

res=filter_test(lambda n:n.endswith('sb'),movie_people)
print(res)

输出

['linhaifeng']
['linhaifeng']

ps4:

# filter函数
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
print(filter(lambda n:not n.endswith('sb'),movie_people))



res=filter(lambda n:not n.endswith('sb'),movie_people)
print(list(res))
print(list(filter(lambda n:not n.endswith('sb'),movie_people)))

输出

<filter object at 0x00000000029A7C88>
['linhaifeng']
['linhaifeng']

reduce函数 

作用:对于序列内所有元素进行累计操作

python内置的几个高阶函数:map() ,reduce(),filter()

示例:循行渐进式演示引出reduce

 

# #求和
num_l=[1,2,3,100]
res=0
for num in num_l:
    res+=num
print(res)

输出  106

num_l=[1,2,3,100]
def reduce_test(array):
    res=0
    for num in array:
        res+=num
    return res
print(reduce_test(num_l))

输出  106

相乘

num_l=[1,2,3,100]
def reduce_test(func,array):
    res=array.pop(0)
    for num in array:
        res=func(res,num)
    return res
print(reduce_test(lambda x,y:x*y,num_l))

输出  600

num_l=[1,2,3,100]
def reduce_test(func,array,init=None):
    if init is None:
        res=array.pop(0)
    else:
        res=init
    for num in array:
        res=func(res,num)
    return res
print(reduce_test(lambda x,y:x*y,num_l,100))

输出  60000

# reduce函数
from functools import reduce
num_l=[1,2,3,100]
print(reduce(lambda x,y:x+y,num_l,1))
print(reduce(lambda x,y:x+y,num_l))

输出

107
106