如何枚举第一个“索引”为 1 的列表?(Python 2.4)

use*_*492 1 python enumerator python-2.4

我需要我的计数器从 1 开始。现在我有

for(counter, file) in enumerate(files):
    counter += 1
    //do stuff with file and counter
Run Code Online (Sandbox Code Playgroud)

但一定有更好的方法,在Python v2.4中

And*_*ker 5

生成器非常适合此目的:

def altenumerate( it ):
    return ((idx+1, value) for idx, value in enumerate(it))
Run Code Online (Sandbox Code Playgroud)

旧版本 python 的简化:

def altenumerateOld( it ):
    idx = 1
    for value in it:
        yield (idx, value)
        idx += 1
Run Code Online (Sandbox Code Playgroud)