python 函數中的遞迴、lambda 、map reduce 等詳解

來源:互聯網
上載者:User

標籤:func   fun   date   before   詳解   rect   cto   lam   gpo   

 

舉例說明

#例1:###遞迴函式求和from traitlets.traitlets import Instancedef mysum(L):    print(L)    if not L:        return 0    else:        return L[0] + mysum(L[1:])  #調用自己 call myselfsum1 = 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)    #改寫方案2def mysum2(L):    return L[0] if len(L) ==1 else L[0] + mysum2(L[1:])sum2 = mysum2([1,2,3])print(‘改寫方案2‘)print(sum2)#改寫方案3def 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 = 0while L:    sum2 += L[0]    L = L[1:]print(‘用while 迴圈實現‘)print(sum2)##用for 迴圈實現L = [1,2,3,4,5,6]sum3 =0for 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 totL = [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+cprint(‘##函數注釋‘)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 +10print(‘##map 在序列中映射函數‘)print(list(map(inc,counter)))print(‘####lambda 也能實現‘)print(list(map(lambda i:i +10,counter)))##from functools import reducere=reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])print(re)import functoolsprint(‘查詢reduce用法‘)help(functools.reduce)##或者from functools import reducehelp(reduce)

上述結果如下

[1, 2, 3, 4][2, 3, 4][3, 4][4][]10#改寫方案128改寫方案26改寫方案315用while 迴圈實現15用for迴圈實現21##任意結構43shixingwenSpam!Ham!##間接調用函數echo##函數注釋6{‘c‘: <class ‘float‘>, ‘a‘: ‘spam‘, ‘b‘: (1, 3)} c => <class ‘float‘>a => spamb => (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 等詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.