標籤:
1
Python支援運行時使用“lambda”建立匿名函數(anonymous functions that are not bound to a name)。
python "lambda"和functional programming語言有區別,但是他非常強大經常拿來和諸如filter(),map(),reduce()
等經典概念結合。
以下樣本普通函數和匿名函數:
1 In [113]: def normalFun (x): return x**22 3 In [114]: print normalFun(8)4 645 6 In [115]: anonymousFun = lambda x:x**27 8 In [116]: print anonymousFun(8)9 64
普通函數和匿名函數運算結果都一樣,但是匿名函數沒有return語句,冒號後邊
運算式就是傳回值。
2 下面程式碼片段展示匿名函數用法,請保證python版本在2.2以上,因為需要支援嵌入範圍。
In [120]: def make_incrementor (n): return lambda x: x + nIn [121]: f = make_incrementor(2)In [122]: g = make_incrementor(6)In [123]: fOut[123]: <function __main__.<lambda>>In [124]: gOut[124]: <function __main__.<lambda>>In [125]: print(42)42In [126]: print f(42)44In [127]: print g(42)48
注意,g,f 定義後類型顯示位<function __main__.<lambda>>,說明此時g,f是匿名函數。
3 以下幾個代碼示範lambda和其他結合用法
In [133]: foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]In [134]: print filter(lambda x: x % 3 == 0, foo)[18, 9, 24, 12, 27]In [135]: ?filterType: builtin_function_or_methodString form: <built-in function filter>Namespace: Python builtinDocstring:filter(function or None, sequence) -> list, tuple, or stringReturn those items of sequence for which function(item) is true. Iffunction is None, return the items that are true. If sequence is a tupleor string, return the same type, else return a list.In [136]: print filter(foo)---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-136-a3d764429161> in <module>()----> 1 print filter(foo)TypeError: filter expected 2 arguments, got 1In [137]: print filter(None,foo)[2, 18, 9, 22, 17, 24, 8, 12, 27]
?mapType: builtin_function_or_methodString form: <built-in function map>Namespace: Python builtinDocstring:map(function, sequence[, sequence, ...]) -> listReturn a list of the results of applying the function to the items ofthe argument sequence(s). If more than one sequence is given, thefunction is called with an argument list consisting of the correspondingitem of each sequence, substituting None for missing values when not allsequences have the same length. If the function is None, return a list ofthe items of the sequence (or a list of tuples if more than one sequence).In [140]: print map(lambda x: x* 2 + 100, foo)[104, 136, 118, 144, 134, 148, 116, 124, 154]In [141]: fooOut[141]: [2, 18, 9, 22, 17, 24, 8, 12, 27]In [142]:
In [146]: print reduce(lambda x,y: x + y, foo)139In [147]: ?reduceType: builtin_function_or_methodString form: <built-in function reduce>Namespace: Python builtinDocstring:reduce(function, sequence[, initial]) -> valueApply 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 itemsof the sequence in the calculation, and serves as a default when thesequence is empty.In [148]: fooOut[148]: [2, 18, 9, 22, 17, 24, 8, 12, 27]
python學習之lambda匿名函數