• python 函数中的递归、lambda 、map reduce 等详解


    举例说明

    #例1:
    ###递归函数求和
    from traitlets.traitlets import Instance
    def mysum(L):
        print(L)
        if not L:
            return 0
        else:
            return L[0] + mysum(L[1:])  #调用自己 call myself
    
    sum1 = mysum([1,2,3,4])
    print(sum1)
    
    
    # 编写替代方案
    def mysum1(L):
        return 0 if not L else L[0] + mysum1(L[1:])
    print('#改写方案1')
    sum1 = mysum1([1,2,3,4,5,6,7])
    print(sum1)    
    
    #改写方案2
    def mysum2(L):
        return L[0] if len(L) ==1 else L[0] + mysum2(L[1:])
    sum2 = mysum2([1,2,3])
    print('改写方案2')
    print(sum2)
    
    #改写方案3
    def mysum3(L):
        first,*rest = L 
        return first if not rest else first + mysum3(rest) ##python3 扩展序列 ext seq 
    sum3 = mysum3([1,2,3,4,5])
    print('改写方案3')
    print(sum3)
    ### 用while 循环实现
    L = [1,2,3,4,5]
    sum2 = 0
    while L:
        sum2 += L[0]
        L = L[1:]
    print('用while 循环实现')
    print(sum2)
    
    ##用for 循环实现
    L = [1,2,3,4,5,6]
    sum3 =0
    for x in L:
        sum3 += x   
    print('用for循环实现') 
    print(sum3)
    
    
    # 处理任意结构
    
    def sumtree(L):
        tot = 0
        for x in L:
            if not isinstance(x,list):
                tot += x
            else:
                tot +=sumtree(L[1:])
        return tot
    L = [1,[2,[3,4],5],6,[7,8]]
    print('##任意结构')
    print(sumtree(L))
    
    ##间接函数调用
    def echo(message):
        print(message)
    
    
    def indirect(func,args):
        func(args)
    indirect(echo, 'shixingwen')
    
    schedule1=[ (echo,'Spam!'),(echo,'Ham!') ]
    for (func,args) in schedule1:
        func(args)
        
    print('##间接调用函数')
    print(echo.__name__)
    
    ###python 函数注解
    
    def func(a:'spam',b:(1,3),c:float):
        return a + b+c
    print('##函数注释')
    print(func(1,2,3))
    zhushi = func.__annotations__
        
    print(zhushi,'
    ')
    for args in zhushi:
        print(args ,'=>', zhushi[args])
        
        
    ##map 在序列中映射函数
    counter = [1,2,3,4]
    update = []
    for i in counter:
        update.append(i+10)
    print('##for循环实现')
    print(update)
    
    
    def inc(i):return i +10
    print('##map 在序列中映射函数')
    print(list(map(inc,counter)))
    print('####lambda 也能实现')
    print(list(map(lambda i:i +10,counter)))
    
    ##
    from functools import reduce
    re=reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
    print(re)
    
    import functools
    print('查询reduce用法')
    help(functools.reduce)
    
    ##或者
    from functools import reduce
    help(reduce)

    上述结果如下

    [1, 2, 3, 4]
    [2, 3, 4]
    [3, 4]
    [4]
    []
    10
    #改写方案1
    28
    改写方案2
    6
    改写方案3
    15
    用while 循环实现
    15
    用for循环实现
    21
    ##任意结构
    43
    shixingwen
    Spam!
    Ham!
    ##间接调用函数
    echo
    ##函数注释
    6
    {'c': <class 'float'>, 'a': 'spam', 'b': (1, 3)} 
    
    c => <class 'float'>
    a => spam
    b => (1, 3)
    ##for循环实现
    [11, 12, 13, 14]
    ##map 在序列中映射函数
    [11, 12, 13, 14]
    ####lambda 也能实现
    [11, 12, 13, 14]
    15
    查询reduce用法
    Help on built-in function reduce in module _functools:
    
    reduce(...)
        reduce(function, sequence[, initial]) -> value
        
        Apply a function of two arguments cumulatively to the items of a sequence,
        from left to right, so as to reduce the sequence to a single value.
        For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
        ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
        of the sequence in the calculation, and serves as a default when the
        sequence is empty.
    
    Help on built-in function reduce in module _functools:
    
    reduce(...)
        reduce(function, sequence[, initial]) -> value
        
        Apply a function of two arguments cumulatively to the items of a sequence,
        from left to right, so as to reduce the sequence to a single value.
        For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
        ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
        of the sequence in the calculation, and serves as a default when the
        sequence is empty.
  • 相关阅读:
    ScrollView阻尼效果
    leetcode ----Trie/stack专题
    【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】
    老板问:多长时间搞定?开发说3天,測试说2天,然后……
    RxJava系列之中的一个 初识Rxjava
    【机器学习具体解释】SVM解二分类,多分类,及后验概率输出
    win8 metro 自己写摄像头录像项目
    战五渣系列之八(绝杀AOP)
    UEFI与MBR区别
    Centos|Rhel搭建vsftp
  • 原文地址:https://www.cnblogs.com/sxwen/p/8034336.html
Copyright © 2020-2023  润新知