使用“for”的计数索引在Python [英] Count indexes using "for" in Python
本文介绍了使用“for”的计数索引在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Python中做同样的:
for(i = 0; i <5; i ++) {cout< i;}
但我不知道如何使用FOR在Python中获取索引
如果你有一些给定的列表,并且想要遍历它的项目和
em>索引,则可以使用 enumerate()
: 用于索引,枚举中的项目(my_list):
打印索引,项目
如果只需要索引,可以使用 range()
:
for i in range(len(my_list)):
print i
I need to do in Python the same as:
for (i = 0; i < 5; i++) {cout << i;}
but I don't know how to use FOR in Python to get the index of the elements in a list.
解决方案
If you have some given list, and want to iterate over its items and indices, you can use enumerate()
:
for index, item in enumerate(my_list):
print index, item
If you only need the indices, you can use range()
:
for i in range(len(my_list)):
print i
这篇关于使用“for”的计数索引在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文