Python enumerate() 函數返回一個枚舉對象。它需要兩個參數,第一個是元素序列,第二個是序列的起始索引。我們可以通過循環或 next() 方法獲取序列元素。
enumerate() 返回的迭代器的 next() 方法返回一個元組和通過迭代序列獲得的值。
簽名
enumerate (sequence, start=0)
參數
sequence:它必須是一個集合或一個序列。它是必需的參數。
start:它是一個可選的索引,用於設置序列的開始。
返回
此方法返回枚舉類型的對象。
讓我們看一些 enumerate() 函數的例子來理解它的函數。
Python enumerate() 函數示例 1
# Python enumerate() function example
# Calling function
result = enumerate([1,2,3])
# Displaying result
print(result)
print(list(result))
輸出:
[(0, 1), (1, 2), (2, 3)]
Python enumerate() 函數示例2
在這裏,我們使用循環獲取元素。請參閱下麵的示例。
# Python enumerate() function example
# Calling function
result = enumerate([1,2,3])
# Displaying result
for element in result:
print(element)
Python enumerate() 函數示例3
在這裏,我們將起始索引作為 10 傳遞。因此,枚舉將從 10 開始到序列中存在的元素。
# Python enumerate() function example
# Calling function
result = enumerate([1,2,3],10)
# Displaying result
for element in result:
print(element)
輸出:
(10, 1) (11, 2) (12, 3)
相關用法
- Python enum.IntEnum用法及代碼示例
- Python enum.auto()用法及代碼示例
- Python enchant.request_dict()用法及代碼示例
- Python enchant.get_enchant_version()用法及代碼示例
- Python enchant.request_pwl_dict()用法及代碼示例
- Python enchant.Dict()用法及代碼示例
- Python enchant.list_languages()用法及代碼示例
- Python enchant.DictWithPWL()用法及代碼示例
- Python enchant.dict_exists()用法及代碼示例
- Python numpy matrix eye()用法及代碼示例
- Python eval()用法及代碼示例
- Python math expm1()用法及代碼示例
- Python Wand evaluate()用法及代碼示例
- Python PIL eval()用法及代碼示例
- Python exec()用法及代碼示例
- Python expandtabs()用法及代碼示例
- Python math exp()用法及代碼示例
- Python numpy matrix empty()用法及代碼示例
- Python numpy.less()用法及代碼示例
- Python Sympy Permutation.list()用法及代碼示例
注:本文由純淨天空篩選整理自 Python enumerate() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。