Python基礎之enumerate列舉

2022-07-18 04:36:09 字數 691 閱讀 9745

列舉,對於乙個可迭代的(iterable)/可遍歷的物件(如列表,字串),enumerate將其組成乙個索引序列,利用它可以同時獲得索引和值。

```python

lst = ["a", "b", "c", "d"]

for i in enumerate(lst):

print(i)

```執行結果為:

```python

(0, 'a')

(1, 'b')

(2, 'c')

(3, 'd')

``````python

lst = ["a", "b", "c", "d"]

for index, name in enumerate(lst, 1):

print(index, name)

```執行結果為:

```python

1 a2 b

3 c4 d

``````python

lst = ["a", "b", "c", "d"]

for index, name in enumerate(lst, 100):

print(index, name)

```執行結果為:

```python

100 a

101 b

102 c

103 d

```

python寫入xlsx遇到enumerate

廢話不多說,直接開幹!官方介紹 enumerate 函式用於將乙個可遍歷的資料物件 如列表 元組或字串 組合為乙個索引序列,同時列出資料和資料下標,一般用在 for 迴圈當中。於是乎又找了這樣介紹 enumerate iteration,start 返回乙個列舉的物件。迭代器 iteration 必...

python基礎之語句 Python基礎之條件語句

我們在程式設計中經常需要通過檢查某個條件,從而決定去做什麼。條件語句就是針對這一情景應用的。本篇主要介紹 if 和 while。一 if語句 先來個總覽 if 條件一 條件一對應的 塊 elif 條件二 條件一對應的 塊 else 不滿足條件一和條件二對應的 塊 if 語句的核心就是值為true 或...

Python程式設計基礎之Python基礎

1.只能是乙個詞 2.包含字母,數字和下劃線 3.不能以數字開頭 this program syas hello and asks for your name print hello world1 print what is your name?ask for their name myname i...