python的内置迭代计数器

2024-09-26 22:42:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想遍历一个列表并引用我所使用的迭代号。我可以用一个简单的计数器来完成,但是有没有一个内置函数?在

List= list(range(0,6))
count = 0
for item in List:
    print "This is interation ", str(count)
    count += 1

Tags: 函数in列表foriscount计数器range
2条回答

这是^{}的目的!在

enumerate(sequence, start=0)

Return an enumerate object. sequence must be a sequence, an iterator, or >some other object which supports iteration.

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

在迭代中,您可以:

^{pr2}$

您应该使用内置函数^{}。在

相关问题 更多 >

    热门问题