Python 中的 map、reduce 和 filter 函式是如何工作的?


在本文中,我們將向您展示

Python 的 map()、filter() 和 reduce() 函式為該語言增添了一絲函數語言程式設計的色彩。這三個函式都是便捷函式,可以用列表推導式或迴圈來代替,但對於某些問題,它們提供了更優雅、更簡潔的解決方案。

map()、filter() 和 reduce() 的工作方式相同。這些函式接受一個函式和一個元素序列,並返回將接收到的函式應用於序列中每個元素的結果。

map() 函式

與 reduce() 類似,map() 函式允許您迭代可迭代物件中的每個專案。另一方面,Map() 對每個專案獨立進行操作,而不是產生單個結果。

最後,map() 函式可用於對兩個或多個列表執行數學運算。它甚至可以用於操作任何型別的陣列。

map() 函式的時間複雜度 = O(n)

語法

map(function, iterable)

引數

  • function − 程式碼中要使用的函式。

  • iterable − 程式碼中迭代的值。

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 建立一個名為 multiplyNumbers 的函式,該函式返回傳遞給它的數字的乘積結果。

  • 在函式內部返回給定數字乘以 3 的結果。

  • 使用 map() 函式將 multiplyNumbers() 函式應用於列表的每個元素,方法是將函式名稱和列表作為引數傳遞給它。

  • 列印將每個元素乘以 3 後的結果列表項。

程式碼

# creating a function that returns multiplication result def multiplyNumbers(givenNumbers): # returning number after multiplying with 3 return givenNumbers*3 # map() function applies the multiplyNumbers function # for each element of the list givenNumbers = map(multiplyNumbers, [1, 3, 5, 2, 6, 10]) # Printing the resultant list items print("Multiplying list elements with 3:") for element in givenNumbers: print(element)

輸出

執行上述程式後,將生成以下輸出 -

Multiplying list elements with 3:
3
9
15
6
18
30

filter() 函式

filter() 函式建立一個新的迭代器,該迭代器從先前建立的迭代器(如列表、元組或字典)中過濾元素。

filter() 函式檢查序列中是否存在給定條件,然後列印結果。

filter() 函式的時間複雜度 = O(n)

語法

filter(function, iterable)

引數

  • function − 程式碼中要使用的函式。

  • iterable − 程式碼中迭代的值。

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 建立一個名為 votingAge 的函式,該函式返回列表中投票的合格年齡。

  • 使用 if 條件語句檢查傳遞給函式的數字是否大於或等於 18。

  • 如果上述語句為真,則返回該數字。

  • 建立一個變數來儲存輸入列表。

  • 使用 filter() 函式,將函式名稱和輸入列表作為引數傳遞給它,以從列表中過濾出大於或等於 18 的年齡。在這裡,它將 votingAge() 函式應用於列表的每個元素,結果僅儲存 votingAge() 函式返回的列表值(此處 votingAge() 函式在數字大於 18 時返回該數字)。

  • 列印過濾器物件

  • 使用 list() 函式(返回可迭代物件的列表)將上述過濾器物件轉換為列表並列印它。

示例

# creating a function that returns the eligibility ages for voting from the list def votingAge(givenNumumber): # checking whether the number is greater than or equal to 18 if givenNumumber>=18: # returning number return givenNumumber # input list inputList = [3, 20, 18, 6, 14, 25, 19] # Getting only values of above list which are greater than or equal to 18 result_filterObj = filter(votingAge, inputList) # printing the filter object print(result_filterObj) # converting into a list print("Eligibility ages for voting from the input list:", list(result_filterObj))

輸出

執行上述程式後,將生成以下輸出 -

<filter object at 0x7fcd3ad14280>
Eligibility ages for voting from the input list: [20, 18, 25, 19]

reduce()

在 Python 中,reduce() 函式迭代列表或其他可迭代資料型別中的每個專案,返回單個值。它位於 functools 庫中。這比迴圈更有效。

語法

reduce(function, iterable)

引數

  • function − 程式碼中要使用的函式。

  • iterable − 程式碼中迭代的值。

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用 import 關鍵字從 functools 模組匯入 reduce() 函式

  • 建立一個名為 addNumbers() 的函式,該函式返回所有列表項的總和。

  • 建立一個變數來儲存輸入列表

  • 使用 reduce() 函式,繞過 addNumbers() 函式,並將輸入列表作為引數以獲取所有列表項的總和。

示例

# importing reduce() function from functools module from functools import reduce # function that returns the sum of all list items def addNumbers(x, y): return x+y # input list inputList = [12, 4, 10, 15, 6, 5] # Print the sum of the list items using reduce() function print("The sum of all list items:") print(reduce(addNumbers, inputList))

輸出

The sum of all list items:
52

當我們將 addNumbers() 函式和輸入列表作為引數傳遞給 reduce() 函式時,它將獲取列表的兩個元素並將它們相加以生成一個元素,然後獲取另一個列表元素並再次將其相加以生成一個元素,依此類推,直到它將列表的所有元素相加並返回一個值。

結論

本文介紹了 map()、reduce() 和 filter() 函式,以及它們的語法和示例。

更新於: 2022-09-15

11K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.