Skip to main content
 首页 » 编程设计

python之在 Python 'for' 中计算项目编号

2025年05月04日18lori

<分区>

Possible Duplicate:
for x in y, type iteration in python. Can I find out what iteration I'm currently on?
Iteration count in python?

有点难以解释,但是当我运行这样的东西时:

fruits = ['apple', 'orange', 'banana', 'strawberry', 'kiwi'] 
 
for fruit in fruits: 
    print fruit.capitalize() 

如预期的那样,它给了我这个:

Apple 
Orange 
Banana 
Strawberry 
Kiwi 

我如何编辑该代码,以便它“计算”它执行 for 的次数,并打印出来?

1 Apple 
2 Orange 
3 Banana 
4 Strawberry 
5 Kiwi