'enumerate()'函數在Python中的用途是什麼?

WBOY
發布: 2023-09-01 11:29:05
轉載
1890 人瀏覽過

enumerate()函數在Python中的用途是什麼?

在本文中,我們將了解 enumerate() 函數以及 Python 中「enumerate()」函數的用途。

什麼是 enumerate() 函數?

Python 的 enumerate() 函數接受資料集合作為參數並傳回一個枚舉物件。

枚舉物件以鍵值對的形式傳回。 key是每個item對應的索引,value是items。

文法

enumerate(iterable, start)
登入後複製
登入後複製

參數

  • iterable - 傳入的資料集合可以作為枚舉物件返回,稱為iterable

  • start - 顧名思義,枚舉物件的起始索引由 start 定義。如果我們忽略這個值,它將認為第一個索引為零,因為零是這裡的預設值

Python 中 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() 函數 -

# 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 內建函數。

  • 此函數傳回一個枚舉物件。

  • 要將可迭代物件傳遞給枚舉函數,首先嘗試從枚舉物件檢索數據,然後將其轉換為 list()。結果,如上面的範例所示,它會傳回一個元組列表以及迭代索引。

結論

本文詳細介紹了 Python enumerate() 函數的應用。我們也學習如何將它與各種 Python 資料類型和迭代一起使用。此外,我們也學習如何修改 enumerate() 函數的開始計數或索引。

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

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