我需要我的计数器从 1 开始。现在我有
for(counter, file) in enumerate(files):
counter += 1
//do stuff with file and counter
但肯定有更好的方法,在 Python v2.4 中
最佳答案
生成器非常适合这个:
def altenumerate( it ):
return ((idx+1, value) for idx, value in enumerate(it))
旧版本 python 的简化:
def altenumerateOld( it ):
idx = 1
for value in it:
yield (idx, value)
idx += 1
关于python - 如何通过列表枚举第一个 "index"报告为 1? ( python 2.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8235833/