Python 的 `enumerate()` 函式有什麼用途?


在本文中,我們將學習 `enumerate()` 函式以及它在 Python 中的用途。

`enumerate()` 函式是什麼?

Python 的 `enumerate()` 函式接收一個數據集合作為引數,並返回一個列舉物件。

返回的列舉物件以鍵值對的形式呈現。鍵是每個專案的相應索引,值是專案本身。

語法

enumerate(iterable, start)

引數

  • iterable − 傳入以便可以作為列舉物件返回的資料集合稱為可迭代物件。

  • start − 正如其名稱所示,列舉物件的起始索引由 `start` 定義。如果忽略此值,則它將第一個索引視為零,因為零是這裡的預設值。

Python 中 `enumerate()` 函式的用途

何時使用 `enumerate()` 函式?

如果需要迭代值的索引,則使用 `enumerate()` 函式。

如何使用它?

`enumerate` 函式為列表中的每個可迭代值分配一個索引。

為什麼要使用 `enumerate()` 函式?

使用迭代器時,我們總是需要知道已完成多少次迭代,即迭代次數。

但是,我們有一種 Pythonic 的方法來確定必要的迭代次數。語法如下:

enumerate(iterable, start)

如何在 Python 中使用 `enumerate()` 函式?

重要的是要注意返回後將使用哪種資料型別來儲存列舉物件。

示例

以下程式在列表上使用 `enumerate()` 函式以獲取包含索引的元組列表:

# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object of the input list
enumerate_obj = enumerate(inputList)
print(enumerate_obj)

# converting the enumerate object into a list and printing it
print(list(enumerate_obj))

輸出

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

<enumerate object at 0x7f4e56b553c0>
[(0, 'hello'), (1, 'tutorialspoint'), (2, 'python'), (3, 'codes')]

使用 `enumerate()` 函式的第二個引數“起始索引”

示例

以下程式演示瞭如何在列表上應用 `enumerate()` 函式,包括第二個引數:

# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object starting from the index 15
enumerate_obj = enumerate(inputList, 15)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

輸出

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

[(15, 'hello'), (16, 'tutorialspoint'), (17, 'python'), (18, 'codes')]

在上面的示例中,我們在 `enumerate()` 函式中添加了 15 作為第二個引數。此第二個引數將指示列舉物件中鍵(索引)的起始索引,因此我們可以看到輸出中的第一個索引為 15,第二個索引為 16,依此類推。

遍歷列舉物件的 Python 程式碼

示例

以下程式演示瞭如何使用 for 迴圈遍歷列舉物件並列印其每個元素:

# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object
enumerate_obj = enumerate(inputList)

# traversing through each item in an enumerate object
for i in enumerate_obj:

    # printing the corresponding iterable item
    print(i)

輸出

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

(0, 'hello')
(1, 'tutorialspoint')
(2, 'python')
(3, 'codes')

在元組上使用 `enumerate()` 函式

示例

以下程式演示瞭如何在給定的輸入元組上使用 `enumerate()` 函式:

# input tuple
inputTuple = ("hello", "tutorialspoint", "python", "codes")

# getting the enumerate object of the input tuple
enumerate_obj = enumerate(inputTuple)
print(enumerate_obj)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

輸出

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

<enumerate object at 0x7fec12856410>
[(0, 'hello'), (1, 'tutorialspoint'), (2, 'python'), (3, 'codes')]

在字串上使用 `enumerate()` 函式

使用 `enumerate` 函式迭代字串資料以確定每個字元的索引。

示例

以下程式演示瞭如何在給定的輸入字串上使用 `enumerate()` 函式:

# input string
inputString = "python"

# getting the enumerate object of an input string
enumerate_obj = enumerate(inputString)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

輸出

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

[(0, 'p'), (1, 'y'), (2, 't'), (3, 'h'), (4, 'o'), (5, 'n')]

在 `enumerate()` 函式中使用另一個引數

示例

以下程式演示瞭如何在 `enumerate()` 函式中使用另一個引數:

input_data = ('Hello', 'TutorialsPoint', 'Website')
for count, dataValue in enumerate(input_data,start = 50):
   print(count, dataValue)

輸出

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

(50, 'Hello')
(51, 'TutorialsPoint')
(52, 'Website')
  • `enumerate` 是 Python 的內建函式,它返回一個列舉物件。

  • 此函式返回一個列舉物件。

  • 要將可迭代物件傳遞給 `enumerate` 函式,首先嚐試從列舉物件中檢索資料,然後將其轉換為 `list()`。結果,如上例所示,它返回一個包含迭代索引的元組列表。

結論

本文詳細介紹了 Python 的 `enumerate()` 函式的應用。我們還學習瞭如何在各種 Python 資料型別和可迭代物件上使用它。此外,我們還學習瞭如何修改 `enumerate()` 函式的起始計數或索引。

更新於:2023年1月16日

瀏覽量:324

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.