W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Python3 enumerate() 函數(shù)用于將一個(gè)可遍歷的數(shù)據(jù)對(duì)象(如列表、元組或字符串)組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。
以下是 enumerate() 方法的語(yǔ)法:
enumerate(sequence, [start=0])
返回 enumerate(枚舉) 對(duì)象。
以下展示了使用 enumerate() 方法的實(shí)例:
>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>>list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>>list(enumerate(seasons, start=1)) # 小標(biāo)從 1 開(kāi)始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
普通的for循環(huán):
>>>i = 0
>>>seq = ['one', 'two', 'three']
>>>for element in seq:
... print(i, seq[i])
... i += 1
...
0 one
1 two
2 three
for循環(huán)使用enumerate:
>>>seq = ['one', 'two', 'three']
>>>for i, element in enumerate(seq):
... print(i, seq[i])
...
0 one
1 two
2 three
>>>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: