×
Python3 教程Python3 基础语法Python3 基本数据类型Python3 解释器Python3 注释Python3 运算符Python3 数字(Number)Python3 字符串Python3 列表Python3 元组Python3 字典Python3 编程第一步Python3 条件控制Python3 循环语句Python3 迭代器与生成器Python3 函数Python3 数据结构Python3 模块Python3 输入和输出Python3 FilePython3 OSPython3 错误和异常Python3 面向对象Python3 标准库概览Python3 实例

Python3 高级教程

Python3 正则表达式Python3 CGI编程Python3 MySQLPython3 网络编程Python3 SMTP发送邮件Python3 多线程Python3 XML解析Python3 JSONPython3 日期和时间Python3 内置函数Python MongoDBPython uWSGI 安装配置

Python3 enumerate() 函数


Python3 内置函数Python3 内置函数


描述

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

语法

以下是 enumerate() 方法的语法:

enumerate(sequence, [start=0])

参数

  • sequence -- 一个序列、迭代器或其他支持迭代对象。
  • start -- 下标起始位置。

返回值

返回 enumerate(枚举) 对象。


实例

以下展示了使用 enumerate() 方法的实例:

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] >>> list(enumerate(seasons, start=1)) # 小标从 1 开始 [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

普通的 for 循环

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

for 循环使用 enumerate

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

Python3 内置函数Python3 内置函数


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)