python 內置&&遞歸
阿新 • • 發佈:2017-05-22
python
優點:
1:可以簡單使用一個腳本來替代我們的函數
2:不用考慮命名的問題
3:簡化代碼的可讀性,不用跳轉到def了,省去這樣的步驟
內置函數:bif
filter:過濾器
map:映射
1 >>> lambda x: 2*x+1 2 <function <lambda> at 0x00000000026C6AC8> 3 >>> g=lambda x: 2*x+1 4 >>> g(3) 5 7 6 >>> help(filter) 7 Help on built-in function filter in module __builtin__: 8 9 filter(...)10 filter(function or None, sequence) -> list, tuple, or string11 12 Return those items of sequence for which function(item) is true. If13 function is None, return the items that are true. If sequence is a tuple14 or string, return the same type, else return a list.15 16 >>> filter(None,[1,0,True,False])17 [1, True]18 >>> 19 >>> 20 >>> tmp=range(10)21 >>> tmp22 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]23 >>> def fun(x):24 return x%225 26 >>> filter(fun,tmp)27 [1, 3, 5, 7, 9]28 >>> list(filter(lambda x:x%2,range(10)))29 [1, 3, 5, 7, 9]30 >>> list(map(lambda x:x*2,range(10)))31 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]32 >>>
遞歸
python限制的遞歸深度大概幾百層,但是可以手動設置
》》》import sys
>>> sys.setrecursionlimit(10000)
python---------------------------------------eg‘1------------------
python 內置&&遞歸