小白教程
所有教程
关于
Search
172.70.100.210
172.70.100.210
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 func enumerate”的源代码
本页内容
上一节:
Python3_func_ascii
下一节:
Python3_func_input
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:enumerate()}}[[Category:python3 built in functions|16]] = Python3 enumerate() 函数 = [[python3 built in functions|Python3 内置函数]] == 描述 == enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。 === 语法 === 以下是 enumerate() 方法的语法: <sample title="" desc="" lang="python" hererun="1"> enumerate(sequence, [start=0]) </sample> === 参数 === * sequence -- 一个序列、迭代器或其他支持迭代对象。 * start -- 下标起始位置。 === 返回值 === 返回 enumerate(枚举) 对象。 以下展示了使用 enumerate() 方法的实例: <sample title="" desc="" lang="python" hererun="1"> >>> 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')] </sample> == 普通的 for 循环 == <sample title="" desc="" lang="python" hererun="1"> i = 0 seq = ['one', 'two', 'three'] for element in seq: print(i, seq[i]) i += 1 </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 0 one 1 two 2 three </sample> == for 循环使用 enumerate == <sample title="" desc="" lang="python" hererun="1"> seq = ['one', 'two', 'three'] for i, element in enumerate(seq): print(i, element) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 0 one 1 two 2 three </sample> [[python3 built in functions|Python3 内置函数]]
返回至“
Python3 func enumerate
”。
上一节:
Python3_func_ascii
下一节:
Python3_func_input