python3 enumerate() 索引迭代
nameList = ['Donn', 'Shirley', 'Ben', 'Janice', 'David','Yen', 'Wendy']
for i,eachLee in enumerate(nameList):
print("%d %s Lee" % (i+1,eachLee))
输出:
1 Donn Lee
2 Shirley Lee
3 Ben Lee
4 Janice Lee
5 David Lee
6 Yen Lee
7 Wendy Lee
本文通过实例演示了Python中enumerate()函数的使用方法,该函数能够同时返回集合中元素的索引和值,使得在遍历集合时可以方便地获取元素的位置信息。示例代码展示了如何使用enumerate()为列表中的每个元素添加索引,并输出带有索引的元素。
338

被折叠的 条评论
为什么被折叠?



