可以使用enumerate()
函数来同时遍历列表的索引和元素。示例如下:
my_list = ["apple", "banana", "cherry"]
for index, value in enumerate(my_list):
print(f"Index: {index}, Value: {value}")
输出结果为:
Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: cherry
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:Python中怎么遍历列表的偶数索引元素