Python 中 map 函式的用途是什麼?
在這篇文章中,我們將學習 Python 中 map 函式的用途。
什麼是 map() 函式?
Python 的 map() 函式將函式應用於作為輸入提供的迭代器的每個專案。列表、元組、集合、字典或字串都可以用作迭代器,它們都返回可迭代的 map 物件。Map() 是 Python 的內建函式。
語法
map(function, iterator1,iterator2 ...iteratorN)
引數
function − map 必須提供一個函式,該函式將應用於迭代器的所有可用專案。
iterator − 必需的可迭代物件。它可以是列表、元組等。map() 函式接受多個迭代器物件作為引數。
返回值
map() 方法將把指定的函式應用於迭代器中的每個專案,併產生一個元組、列表或其他可迭代的 map 物件。
map() 函式的工作原理?
map() 函式的兩個輸入是函式和可迭代物件。傳遞給 map() 的函式是一個普通函式,它將迴圈遍歷指定可迭代物件中的每個值。
使用 map() 和數字列表
示例
以下程式使用 Python 中的 map() 函式將 5 加到列表中的每個元素。
# creating a function that accepts the number as an argument def exampleMapFunction(num): # adding 5 to each number in a list and returning it return num+5 # input list inputList = [3, 5, 1, 6, 10] # Passing above defined exampleMapFunction function # and given list to the map() function # Here it adds 5 to every element of the given list modifiedList = map(exampleMapFunction, inputList) # printing the modifies list(map object) print(modifiedList) # converting the map object to the list and printing it print("Adding 5 to each element in a list using map():\n", list(modifiedList))
輸出
<map object at 0x7fb106076d10> Adding 5 to each element in a list using map(): [8, 10, 6, 11, 15]
使用 map() 和字典
Python 使用字典來實現更常用地稱為關聯陣列的內容。字典是鍵值對的集合。它使用花括號 () 定義。
字典是動態且變化的。可以根據需要更改和刪除它們。字典項可以使用鍵訪問,但列表元素是透過索引在其列表中的位置檢索的,這就是字典與列表的不同之處。
由於字典是迭代器,因此可以在 map() 函式內使用它。
示例
以下程式使用 Python 中的 map() 函式將 5 加到字典中的每個元素。
# creating a function that accepts the number as an argument def exampleMapFunction(num): # adding 5 to each number in a dictionary and returning it return num + 5 # input Dictionary inputDictionary = {2, 3, 4, 5, 6, 7, 8, 9} # passing above defined exampleMapFunction function # and input dictionary to the map() function # Here it adds 5 to every element of the given dictionary modifiedDict = map(exampleMapFunction, inputDictionary) # printing the modified dictionary(map object) print(modifiedDict) # converting the map object to the list and printing it print("Adding 5 to each element in a dictionary using map():\n", list(modifiedDict))
輸出
<map object at 0x7fb1060838d0> Adding 5 to each element in a dictionary using map(): [7, 8, 9, 10, 11, 12, 13, 14]
使用 map() 和元組
在 Python 中,元組是一個物件,其元素用逗號分隔並用圓括號括起來。
示例
以下程式碼使用 lower() 和 map() 函式將元組中的所有專案轉換為小寫。
# creating a function that accepts the number as an argument def exampleMapFunction(i): # converting each item in tuple into lower case return i.lower() # input tuple inputTuple = ('HELLO', 'TUTORIALSPOINT', 'pyTHON', 'CODES') # passing above defined exampleMapFunction function # and input tuple to the map() function # Here it converts every element of the tuple to lower case modifiedTuple = map(exampleMapFunction, inputTuple) # printing the modified tuple(map object) print(modifiedTuple) print('Converting each item in a tuple to lowercase:') # converting the map object to the list and printing it print(list(modifiedTuple))
輸出
<map object at 0x7fb10f773590> Converting each item in a tuple to lowercase: ['hello', 'tutorialspoint', 'python', 'codes']
在 Python 中使用 map() 和其他函式工具
使用 map() 以及filter() 和reduce() 等函式工具,我們可以對可迭代物件執行更復雜的更改。
使用 map() 和 filter()
在某些情況下,我們必須處理可迭代輸入並透過從輸入中刪除/過濾不必要的專案來返回另一個可迭代物件。在這種情況下,Python 的filter() 是一個明智的選擇。
filter() 函式返回函式返回true 的可迭代輸入專案。
如果沒有傳遞函式,則 filter() 使用恆等函式。這意味著 filter() 檢查可迭代項中的每個專案的真值,並刪除所有假值。
示例
以下函式過濾列表中的所有正數,並使用 filter() 和 map() 函式一起返回它們的平方根。
# importing math module import math # creating a function that returns whether the number # passed is a positive number or not def isPositive(n): return n >= 0 # creating a function that filters all the positive numbers # from the list and returns the square root of them. def filterSqrtofPositive(nums): # filtering all the positive numbers from the list using filter() # and returning the square root of them using the math.sqrt and map() filteredItems = map(math.sqrt, filter(isPositive, nums)) # returning the list of filetred elements return list(filteredItems) # input list inputList= [16, -10, 625, 25, -50, -25] # calling the function by passing the input list print(filterSqrtofPositive(inputList))
輸出
[4.0, 25.0, 5.0]
結論
Python 的 map() 函式允許您對可迭代物件執行操作。Map() 常用於轉換和處理可迭代物件,而無需迴圈。
在這篇文章中,我們學習瞭如何透過使用幾個資料型別作為示例來在 Python 中使用 map() 方法。