python - 如何通过列表枚举第一个 "index"报告为 1? ( python 2.4)

标签 python python-2.4 enumerator

我需要我的计数器从 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/

相关文章:

python - Polar 日期 YYYY 周

Python 尝试/除了不工作

ruby - 在 Ruby 中尽早转义 .each { } 迭代

playframework - Play 框架 [2.2-scala] : creating Enumerator from a slow InputStream

python - 无法在 suds 中创建 SOAP 过滤器

python - 类型错误 : __init__() missing 1 required positional argument: 'figure'

c# - 如何一次遍历两个数组?

python - 正则表达式错误 : raise error, v # 无效表达式

Python max() 没有关键字参数