目錄
什麼是map()函數?
文法
參數
傳回值
map() 函數如何運作?
使用帶有數字列表的map()
範例
輸出
使用map()與字典
使用 map() 函數與元組
在Python中使用map()與其他函數工具
使用map()和filter()函數
結論
首頁 後端開發 Python教學 Python中的map函數有什麼用途?

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

Sep 09, 2023 pm 07:05 PM
python 用途 map函數

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

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

什麼是map()函數?

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

文法

1

map(function, iterator1,iterator2 ...iteratorN)

登入後複製

參數

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

  • iterator - 強制可迭代物件。它可以是列表、元組等。 map() 函數接受多個迭代器物件作為參數。

傳回值

map() 方法會將指定的函數套用至迭代器中的每個項目,並產生一個元組、列表或另一個可迭代的映射物件。

map() 函數如何運作?

函數和可迭代物件是map()函數的兩個輸入。傳遞給map()的函數是一個普通函數,它將遍歷指定可迭代物件中的每個值。

使用帶有數字列表的map()

範例

以下程式使用 Python 中的 map() 函數將 5 加到列表中的每個元素 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

登入後複製

輸出

1

2

3

<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 加入字典中的每個元素 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

登入後複製

輸出

1

2

3

<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()函數將元組中的所有項轉換為小寫:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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

登入後複製

輸出

1

2

3

<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()函數過濾列表中的所有正數並傳回它們的平方根 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

登入後複製

輸出

1

[4.0, 25.0, 5.0]

登入後複製

結論

Python 的 map() 函數可讓您對可迭代物件執行操作。 Map() 通常用於轉換和處理可迭代對象,而不需要循環。

在本文中,我們以幾種資料型別為例,學習如何在 Python 中使用 map() 方法。

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

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱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

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1274
29
C# 教程
1256
24
PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

sublime怎麼運行代碼python sublime怎麼運行代碼python Apr 16, 2025 am 08:48 AM

在 Sublime Text 中運行 Python 代碼,需先安裝 Python 插件,再創建 .py 文件並編寫代碼,最後按 Ctrl B 運行代碼,輸出會在控制台中顯示。

PHP和Python:深入了解他們的歷史 PHP和Python:深入了解他們的歷史 Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

Python vs. JavaScript:學習曲線和易用性 Python vs. JavaScript:學習曲線和易用性 Apr 16, 2025 am 12:12 AM

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Golang vs. Python:性能和可伸縮性 Golang vs. Python:性能和可伸縮性 Apr 19, 2025 am 12:18 AM

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

vscode在哪寫代碼 vscode在哪寫代碼 Apr 15, 2025 pm 09:54 PM

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

notepad 怎麼運行python notepad 怎麼運行python Apr 16, 2025 pm 07:33 PM

在 Notepad 中運行 Python 代碼需要安裝 Python 可執行文件和 NppExec 插件。安裝 Python 並為其添加 PATH 後,在 NppExec 插件中配置命令為“python”、參數為“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通過快捷鍵“F6”運行 Python 代碼。

See all articles