亚洲免费一级高潮_欧美极品白嫩视频在线_中国AV片在线播放_欧美亚洲日韩欧洲在线看

您現(xiàn)在所在的位置:首頁(yè) >學(xué)習(xí)資源 > Python全棧+人工智能入門教材 > Python基礎(chǔ)入門教程59:Python enumerate() 函數(shù)

Python基礎(chǔ)入門教程59:Python enumerate() 函數(shù)

來(lái)源:奇酷教育 發(fā)表于:

  enumerate() 函數(shù)用于將一個(gè)可遍歷的數(shù)據(jù)對(duì)象(如列表、元組或字符串)組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。

  Python 內(nèi)置函數(shù)

  描述

  enumerate() 函數(shù)用于將一個(gè)可遍歷的數(shù)據(jù)對(duì)象(如列表、元組或字符串)組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。

  Python 2.3. 以上版本可用,2.6 添加 start 參數(shù)。

  語(yǔ)法

  以下是 enumerate() 方法的語(yǔ)法:

  enumerate(sequence, [start=0])

  參數(shù)

  sequence -- 一個(gè)序列、迭代器或其他支持迭代對(duì)象。

  start -- 下標(biāo)起始位置。

  返回值

  返回 enumerate(枚舉) 對(duì)象。

  實(shí)例

  以下展示了使用 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 開始 [(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 threefor 循環(huán)使用 enumerate

  >>>seq = ['one', 'two', 'three'] >>> for i, element in enumerate(seq): ... print i, seq[i] ... 0 one 1 two 2 three >>>