#map函数
#例子1
num_1=[1,2,10,5,3,7]
ret=[]
for i in num_1:
    ret.append(i**2)
print(ret)          #完成一个,

  



#例子2
num_1=[1,2,10,5,3,7]
def map_test(array):    #array,数组
    ret = []
    for i in num_1:
        ret.append(i ** 2)
    return ret
ret=map_test(num_1)    #可以完成一类
print(ret)

  



#例子3
num_1=[1,2,10,5,3,7]
# lambda x:x+1   #和add_one函数效果是一样的
def add_one(x):
    return x+1           #自增1

#lambda x:x-1        #和reduce_one函数效果是一样的
def reduce_one(x):
    return x-1              #自减1

#lambda x:x**2      #和pf函数效果是一样的
def pf(x):
    return x**2

def map_test(func,array):
    ret=[]
    for i in array:
        res=func(i)
        ret.append(res)
    return ret
print(map_test(add_one,num_1))     #把方法和参数一起传给他
print(map_test(lambda x:x+1,num_1))
print(map_test(reduce_one,num_1))
print(map_test(lambda x:x-1,num_1))
print(map_test(pf,num_1))
print(map_test(lambda x:x**2,num_1))

res=map(lambda x:x+1,num_1)  #map函数,一个是处理方法(可以是lambda,也可以是自定义的),一个是可迭代对象
print("内置函数map处理结果",res)
print(list(res))
print("自定义函数",list(map(reduce_one,num_1)))   #函数式编程,运行比较简单,可读性比较差

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

  



#filter函数
#把带sha开头的都清理出去
# 1.不用函数
movie_people=['sha_guanyu','sha_zhangfei','zhaoyun',"sha_liubei"]
ret=[]
for p in movie_people:
    if not p.startswith('sha'):
        ret.append(p)
print(ret)

  



# 2.用函数的方法
movie_people=['sha_guanyu','sha_zhangfei','zhaoyun',"sha_liubei"]
def filter_test(array):
    ret = []
    for p in array:
        if not p.startswith('sha'):
            ret.append(p)
    return ret
print(filter_test(movie_people))   #filter函数

  



#3.如果sha在结尾,或者中间怎么样办?guanyu_sha
movie_people=['guanyu_sha','zhangfei_sha','zhaoyun',"liubei_sha"]
def sha_show(n):
    return n.endswith('sha')

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

  



#4.用lambda
movie_people=['guanyu_sha','zhangfei_sha','zhaoyun',"liubei_sha"]

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('sha'),movie_people)
print(res)

  



#4.用filter函数
movie_people=['guanyu_sha','zhangfei_sha','zhaoyun',"liubei_sha"]
print(filter(lambda n:n.endswith('sha'),movie_people))
print(list(filter(lambda n:n.endswith('sha'),movie_people)))
print(list(filter(lambda n:not n.endswith('sha'),movie_people)))

  


# reduce函数
from functools import reduce

#把列表里面的所有的数都加起来
#1.方法一
num_l=[1,2,3,4,5,6,100]
res=0
for num in num_l:
    res+=num
print(res)

  



#2.用函数的形式
num_l=[1,2,3,4,5,6,100]
def reduce_test(array):
    res=0
    for num in array:
        res+=num
    return res
print(reduce_test(num_l))

  



#3.用函数的形式,算出列表里面相乘的结果
num_l=[1,2,3,100]
# def multi(x,y):
#     return x*y
# lambda x,y:x*y
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))

  



#4.指定初始第一个值
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))

  




#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))

  



# map()
#处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数及位置与原来一样


#filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来

people=[
    {'name':'alex','age':1000},
    {'name':'wupei','age':10000},
    {'name':'yuanhao','age':9000},
    {'name':'linhaifeng','age':18},
]
print(list(filter(lambda p:p['age']<=18,people)))

  



#reduce:处理一个序列,然后把序列进行合并操作
from functools import reduce
print(reduce(lambda x,y:x+y,range(100),100))
print(reduce(lambda x,y:x+y,range(1,101)))

  

posted on 2024-08-14 17:27  柳志军  阅读(23)  评论(0)    收藏  举报