# 示例代码:使用 enumerate 函数
# enumerate 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,
# 同时列出数据和数据下标,一般用在 for 循环中。
# 示例 1:遍历列表并获取元素及其索引
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
# 输出:
# Index 0: apple
# Index 1: banana
# Index 2: cherry
# 示例 2:指定起始索引
for index, fruit in enumerate(fruits, start=1):
print(f"Item {index}: {fruit}")
# 输出:
# Item 1: apple
# Item 2: banana
# Item 3: cherry
上一篇:python 日期函数
下一篇:pythondef函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站