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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,\n)
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.

 

以上是关于python 函数中的递归lambda map reduce 等详解的主要内容,如果未能解决你的问题,请参考以下文章

18.07.20(lambda().sorted().filter().map().递归.二分查找)

20180720 (lambda匿名函数,sorded()排序函数,filter()筛选函数,map()映射函数,递归,二分函数)

递归,二分法,lambda,filter,map,sorted

day05 协程函数,递归函数,匿名函数lambda,内置函数map reduce filter max min zip sorted,匿名函数lambda和内置函数结合使用,面向过程编程与函数编程

Python 之 map函数 及lambda函数

1. lamda匿匿名函数 2. sorted() 3. ?lter() 4. map() 5. 递归函数