日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

python?使用enumerate()函數(shù)詳解_python

作者:凌冰_ ? 更新時間: 2022-12-04 編程語言

一、enumerate() 函數(shù)簡介

enumerate()是python的內(nèi)置函數(shù),將一個可遍歷iterable數(shù)據(jù)對象(如list列表、tuple元組或str字符串)組合為一個索引序列,同時列出數(shù)據(jù)和數(shù)據(jù)下標,一般用在for循環(huán)當中。
函數(shù)返回一個enumerate對象,是一個可迭代對象。具體元素值可通過遍歷取出。
函數(shù)語法為:

語法: enumerate(sequence, [start=0])

參數(shù)
sequence -- 一個序列、迭代器或其他支持迭代對象。
start -- 下標起始位置。
返回值
返回 enumerate(枚舉) 對象。

函數(shù)參數(shù)有:

  • sequence是一個可迭代對象
  • start是一個可選參數(shù),表示索引從幾開始計數(shù)

二、使用enumerate()函數(shù)

(1)使用for循環(huán)

1、迭代列表時如何訪問列表下標索引
ll=[22, 36, 54, 41, 19, 62, 14, 92, 17, 67]
for i in range(len(ll)):
    print(i, "=", ll[i])

(2)使用enumerate()

# 優(yōu)雅版:
for index,item in enumerate(ll):
    print(index, "=",item)

此外,enumerate()函數(shù)還有第二個參數(shù),用于指定索引的起始值

# 優(yōu)雅版:
for index,item in enumerate(ll,10):
    print(index, "=",item)

原文鏈接:https://blog.csdn.net/hlx20080808/article/details/127609393

欄目分類
最近更新