搜尋
首頁後端開發Python教學Python中的map函數有什麼用途?

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))

輸出

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

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

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

最大化2小時:有效的Python學習策略最大化2小時:有效的Python學習策略Apr 20, 2025 am 12:20 AM

在兩小時內高效學習Python的方法包括:1.回顧基礎知識,確保熟悉Python的安裝和基本語法;2.理解Python的核心概念,如變量、列表、函數等;3.通過使用示例掌握基本和高級用法;4.學習常見錯誤與調試技巧;5.應用性能優化與最佳實踐,如使用列表推導式和遵循PEP8風格指南。

在Python和C之間進行選擇:適合您的語言在Python和C之間進行選擇:適合您的語言Apr 20, 2025 am 12:20 AM

Python適合初學者和數據科學,C 適用於系統編程和遊戲開發。 1.Python簡潔易用,適用於數據科學和Web開發。 2.C 提供高性能和控制力,適用於遊戲開發和系統編程。選擇應基於項目需求和個人興趣。

Python與C:編程語言的比較分析Python與C:編程語言的比較分析Apr 20, 2025 am 12:14 AM

Python更適合數據科學和快速開發,C 更適合高性能和系統編程。 1.Python語法簡潔,易於學習,適用於數據處理和科學計算。 2.C 語法複雜,但性能優越,常用於遊戲開發和系統編程。

每天2小時:Python學習的潛力每天2小時:Python學習的潛力Apr 20, 2025 am 12:14 AM

每天投入兩小時學習Python是可行的。 1.學習新知識:用一小時學習新概念,如列表和字典。 2.實踐和練習:用一小時進行編程練習,如編寫小程序。通過合理規劃和堅持不懈,你可以在短時間內掌握Python的核心概念。

Python與C:學習曲線和易用性Python與C:學習曲線和易用性Apr 19, 2025 am 12:20 AM

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!