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