lambda 匿名函數
- 什么是lambda?
lambda 操作符(或 lambda 函數)通常用來創建小巧的,一次性的匿名函數對象。它的基本語法如下:
lambda arguments : expression
lambda 操作符可以有任意數量的參數,但是它只能有一個表達式,且不能包含任何語句,返回一個可以賦值給任何變量的函數對象。
- lambda 匿名函數示例:
In [73]: add = lambda x, y : x + y
In [74]: add
Out[74]: <function __main__.<lambda>(x, y)>
In [75]: print(add(1, 2))
3
- lambda 匿名函數理解:
在lambda x, y : x + y中,x和y是函數的參數,x+y是表達式,它被執行並返回結果。
lambda x, y : x + y返回的是一個函數對象,它可以被賦值給任何變量。在本例中函數對象被賦值給了 add 變量。如果我們查看 add 的 type,可以看到它是一個 Function。
type(add)
Output: function
絕大多數 lambda 函數作為一個參數傳給一個需要函數對象為參數的函數,比如 map,reduce,filter 等函數。
map
-
map的基本語法如下:
map(function_object, iterable1, iterable2, ...)
map函數需要一個函數對象和任意數量的 iterables,如 list、dictionary 等。它為序列中的每個元素執行 function_object,並返回由函數對象修改的元素組成的列表。 -
map 示例如下:
In [64]: def add(x):
...: return x+2
...:
...:
In [65]: list(map(add, [1,2,3,4]))
Out[65]: [3, 4, 5, 6]
在上面的例子中,map 對 list 中的每個元素1,2,3,4執行 add 函數並返回[3,4,5,6]。接着看看,如何用 map 和 lambda 重寫上面的代碼:
In [67]: list(map(lambda x: x+2, [1,2,3,4]))
Out[67]: [3, 4, 5, 6]
- 使用 map 和 lambda 迭代 dictionary:
In [70]: dict_a = [
...: {'name': 'python', 'points': 10},
...: {'name': 'java', 'points': 8}
...: ]
In [71]: list(map(lambda x : x['name'], dict_a))
Out[71]: ['python', 'java']
以上代碼中,dict_a 中的每個dict作為參數傳遞給 lambda 函數。 lambda 函數表達式作用於每個 dict 的結果作為輸出。
- map 函數作用於多個 iterables:
In [53]: list_a = [1, 2, 3]
In [54]: list_b = [10, 20, 30]
In [55]: list(map(lambda x, y: x + y, list_a, list_b))
Out[55]: [11, 22, 33]
這里,list_a 和 list_b 的第 i 個元素作為參數傳遞給 lambda 函數。
在 Python3 中,map 函數返回一個惰性計算(lazily evaluated)的迭代器(Iterator)或 map 對象。就像 zip 函數是惰性計算那樣。
我們不能通過 index 訪問 map 對象的元素,也不能使用 Len() 得到它的長度。
但我們可以強制轉換 map 對象為 list:
In [47]: map_output = map(lambda x: x*2, [1, 2, 3, 4])
In [48]: map_output
Out[48]: <map at 0x1024e4a90>
In [49]: list(map_output)
Out[49]: [2, 4, 6, 8]
filter
- filter 的基本語法如下:
filter(function_object, iterable)
filter 函數需要兩個參數,function_object 返回一個布爾值(boolean),對 iterable 的每一個元素調用 function_object,filter 只返回滿足 function_object 為 True 的元素。
和 map 函數一樣,filter 函數也返回一個 list,但與 map 函數不同的是,filter 函數只能有一個 iterable 作為輸入。
- 過濾出奇數
In [45]: def is_even(x):
...: return x & 1 != 0
...:
...:
...:
In [46]: list(filter(is_even, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Out[46]: [1, 3, 5, 7, 9]
- 過濾 dicts 的 list:
In [43]: dict_a = [
...: {'name': 'python', 'points': 10},
...: {'name': 'java', 'points': 8}
...: ]
In [44]: list(filter(lambda x : x['name'] == 'python', dict_a))
Out[44]: [{'name': 'python', 'points': 10}]
和 map 一樣,filter 函數在 Python3 中返回一個惰性計算的 filter 對象或迭代器。我們不能通過 index 訪問 filter 對象的元素,也不能使用 Len() 得到它的長度。
In [37]: list_a = [1, 2, 3, 4, 5]
In [38]: filter_obj = filter(lambda x: x % 2 == 0, list_a)
In [39]: even_num = list(filter_obj)
In [40]: even_num
Out[40]: [2, 4]
reduce 函數
- 什么是 reduce 函數?
reduce 函數會對參數序列中元素進行累積。
reduce 函數語法:
reduce(function, sequence[, initial]) -> value
function 參數是一個有兩個參數的函數,reduce 依次從 sequence 中取一個元素,和上一次調用 function 的結果做參數再次調用 function。
第一次調用 function 時,如果提供 initial 參數,會以 sequence 中的第一個元素和 initial 作為參數調用 function,否則會以序列 sequence 中的前兩個元素做參數調用 function。
- reduce 示例:
In [34]: from functools import reduce
...: reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1)
...:
Out[34]: 21
In [35]: reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])
Out[35]: 20
注意 function 函數不能為 None。
reversed
- 什么是 reversed
reversed()函數是返回序列seq的反向訪問的迭代子。參數可以是列表,元組,字符串,不改變原對象。
reversed()之后,只在第一次遍歷時返回值。 - reversed 示例:
In [1]: a_list = [1, 2, 3, 4, 5]
In [2]: new_list = reversed(a_list)
In [3]: a_list
Out[3]: [1, 2, 3, 4, 5]
In [4]: new_list
Out[4]: <list_reverseiterator at 0x10d714f28>
In [5]: for i in new_list:
...: print(i)
...:
5
4
3
2
1
In [6]: for i in new_list:
...: print(i)
...:
In [7]:
參考資料:
https://www.toutiao.com/a6612880904165523982/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1×tamp=1539761465&app=news_article&utm_source=weixin&iid=46504004486&utm_medium=toutiao_android&group_id=6612880904165523982
https://blog.csdn.net/sxingming/article/details/51353379