python學習手冊中的一些易忘的點(4-7部分)

來源:互聯網
上載者:User

標籤:高彙總低耦合   高彙總   類型   改變   lis   lse   reduce   使用   統一   

有時間系統看一遍python學習手冊,記錄一些易忘點

1.python函數高彙總低耦合
1)對於輸入使用參數並且對於輸出使用return
2)只有在真正必要的情況下使用全域變數
3)不要改變可變類型的參數,除非調用者希望這樣做
4)每個函數都應該有一個單一的、統一的目標
5)每一個函數應該相對較小
6)避免直接改變在另一個檔案中的變數

2.遞迴處理任意結構
>>> a=[3,[2,[2,3,4],2],1,[1,5,[1,3,3]]]
>>> def sumtree(L):
...   tot=0
...   for x in L:
...     if not isinstance(x,list):
...       tot+=x
...     else:
...       tot+=sumtree(x)
...   return tot
...
>>> sumtree(a)
30
>>>

3.lamdba,map,filter,reduce用法
>>> map((lambda x: x+3),[1,2,3,4,5,6])
[4, 5, 6, 7, 8, 9]
>>> map((lambda x,y: x+y),[1,2,3,4,5,6],[2,3,4,5,6,7])
[3, 5, 7, 9, 11, 13]
>>>
>>> filter((lambda x: x%2==0),[1,2,3,4,5,6])
[2, 4, 6]
>>> reduce((lambda x,y: x+y),[1,2,3,4,5,6])
21
>>> reduce((lambda x,y: x*y),[1,2,3,4,5,6])
720
>>>

 

>>> filter((lambda x: x%2==0),[1,2,3,4,5,6])
[2, 4, 6]
>>> reduce((lambda x,y: x+y),[1,2,3,4,5,6])
21
>>> reduce((lambda x,y: x*y),[1,2,3,4,5,6])
720
>>>

python學習手冊中的一些易忘的點(4-7部分)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.