# Python `enumerate()`函數
> 原文: [https://thepythonguru.com/python-builtin-functions/enumerate/](https://thepythonguru.com/python-builtin-functions/enumerate/)
* * *
于 2020 年 1 月 7 日更新
* * *
`enumerate()`函數采用一個可迭代對象,并返回一個枚舉對象(一個迭代器),該對象生成一個格式為`(index, item)`的元組,其中`index`指該項目的偏移量,`item`指的是可迭代對象中的對應的項目。
`enumerate()`函數的語法如下:
**語法**:
```py
enumerate(iterable[, start=0]) -> iterator for index, value of iterable
```
| 參數 | 描述 |
| --- | --- |
| `iterable` | (必需)任何可迭代的對象,例如字符串,列表,字典等。 |
| `start`(可選) | `index`的初始值。 默認為`0`。 |
這是一個例子:
```py
>>>
>>> list(enumerate("hello"))
[(0, 'h'), (1, 'e'), (2, 'l'), (3, 'l'), (4, 'o')]
>>>
>>>
>>> for index, value in enumerate("hello"):
... print(index, value)
...
0 h
1 e
2 l
3 l
4 o
>>>
```
試試看:
```py
print( list(enumerate("hello")) )
for index, value in enumerate("hello"):
print(index, value)
```
以下清單顯示`enumerate()`如何與清單,字典和元組一起使用:
```py
>>>
>>> for index, value in enumerate([110, 45, 12, 891, "one"]):
... print(index, value)
...
0 110
1 45
2 12
3 891
4 one
>>>
>>>
>>> for index, value in enumerate({'name': 'Jane', 'age': 26, 'salary': 40000}):
... print(index, value)
...
0 name
1 salary
2 age
>>>
>>>
>>> for index, value in enumerate({1, 290, -88, 10}):
... print(index, value)
...
0 -88
1 1
2 10
3 290
>>>
```
試一試:
```py
for index, value in enumerate([110, 45, 12, 891, "one"]):
print(index, value)
print("-"*20)
for index, value in enumerate({'name': 'Jane', 'age': 26, 'salary': 40000}):
print(index, value)
print("-"*20)
for index, value in enumerate({1, 290, -88, 10}):
print(index, value)
```
## 設置索引的初始值
* * *
要設置索引的初始值,我們使用`start`關鍵字參數。
```py
>>>
>>> list(enumerate("hello", start=2))
[(2, 'h'), (3, 'e'), (4, 'l'), (5, 'l'), (6, 'o')]
>>>
>>>
>>> for index, value in enumerate("hello", start=2):
... print(index, value)
...
2 h
3 e
4 l
5 l
6 o
>>>
```
試一試:
```py
print( list(enumerate("hello", start=2)) )
for index, value in enumerate("hello", start=2):
print(index, value)
```
* * *
* * *
- 初級 Python
- python 入門
- 安裝 Python3
- 運行 python 程序
- 數據類型和變量
- Python 數字
- Python 字符串
- Python 列表
- Python 字典
- Python 元組
- 數據類型轉換
- Python 控制語句
- Python 函數
- Python 循環
- Python 數學函數
- Python 生成隨機數
- Python 文件處理
- Python 對象和類
- Python 運算符重載
- Python 繼承與多態
- Python 異常處理
- Python 模塊
- 高級 Python
- Python *args和**kwargs
- Python 生成器
- Python 正則表達式
- 使用 PIP 在 python 中安裝包
- Python virtualenv指南
- Python 遞歸函數
- __name__ == "__main__"是什么?
- Python Lambda 函數
- Python 字符串格式化
- Python 內置函數和方法
- Python abs()函數
- Python bin()函數
- Python id()函數
- Python map()函數
- Python zip()函數
- Python filter()函數
- Python reduce()函數
- Python sorted()函數
- Python enumerate()函數
- Python reversed()函數
- Python range()函數
- Python sum()函數
- Python max()函數
- Python min()函數
- Python eval()函數
- Python len()函數
- Python ord()函數
- Python chr()函數
- Python any()函數
- Python all()函數
- Python globals()函數
- Python locals()函數
- 數據庫訪問
- 安裝 Python MySQLdb
- 連接到數據庫
- MySQLdb 獲取結果
- 插入行
- 處理錯誤
- 使用fetchone()和fetchmany()獲取記錄
- 常見做法
- Python:如何讀取和寫入文件
- Python:如何讀取和寫入 CSV 文件
- 用 Python 讀寫 JSON
- 用 Python 轉儲對象