Python中的map函數有什麼用途?

WBOY
發布: 2023-09-09 19:05:03
轉載
3200 人瀏覽過

Python中的map函數有什麼用途?

在本文中,我們將學習Python中map函數的使用。

什麼是map()函數?

Python的map()函數將一個函數應用於作為輸入提供的迭代器中的每個項目。列表、元組、集合、字典或字串都可以用作迭代器,並且它們都傳回可迭代的map物件。 map()是Python的內建函數。

文法

map(function, iterator1,iterator2 ...iteratorN)
登入後複製

參數

  • function - 有必要提供一個帶有函數的映射,該函數將應用於迭代器的所有可用項目。

  • iterator - 強制可迭代物件。它可以是列表、元組等。 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))
登入後複製

輸出


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))
登入後複製

輸出


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))
登入後複製

輸出


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() 方法。

以上是Python中的map函數有什麼用途?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!