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中
生成器非常适合此目的:
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)
| 归档时间: |
|
| 查看次数: |
325 次 |
| 最近记录: |