zoukankan      html  css  js  c++  java
  • day16__高阶函数

    匿名函数:

      lambda

       lambda x:x+1

      func = lambda x:x+1
      print(func(10))

      

           

     高阶函数

      满足俩个特性任意一个即为高阶函数

        1.函数的传入参数是一个函数名

        2.函数的返回值是一个函数名

    map:

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

      

    num_l=[1,2,10,5,3,7]
    res=map(lambda x:x+1,num_l)    #[2, 3, 11, 6, 4, 8]
    
    
    msg='linhaifeng'
    print(list(map(lambda x:x.upper(),msg)))  # ['L', 'I', 'N', 'H', 'A', 'I', 'F', 'E', 'N', 'G']
    

      

    filter:

    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']
    

     

    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']
    

      

    #终极版本
    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']
    

      

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

      

     

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

      

    reduce:

    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))   #107
    print(reduce(lambda x,y:x+y,num_l))     #106
    #print(reduce(function,sequence,initial))   #函数,序列,初始值
    

      

  • 相关阅读:
    关于设计
    开机速度优化
    寻找发帖“水王”《编程之美》笔记
    SPSS学习笔记
    《你的灯亮着吗》读书笔记
    《遇见未知的自己》读书笔记
    Python中字符串与字典间转换
    ide vim 设置zz
    Redis几个认识误区zz
    探索AJAX中的消息传输模式(二)
  • 原文地址:https://www.cnblogs.com/augustyang/p/9035115.html
Copyright © 2011-2022 走看看