简体   繁体   English

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

[英]How to enumerate through list with the first "index" reported as 1? (Python 2.4)

I need my counter to start at 1. Right now I have我需要我的计数器从 1 开始。现在我有

for(counter, file) in enumerate(files):
    counter += 1
    //do stuff with file and counter

But there must be a better way, in Python v2.4但是一定有更好的方法,在Python v2.4

Generators are perfect for this: 生成器非常适合:

def altenumerate( it ):
    return ((idx+1, value) for idx, value in enumerate(it))

A simplified for older versions of python: 较旧版本的python的简化版本:

def altenumerateOld( it ):
    idx = 1
    for value in it:
        yield (idx, value)
        idx += 1

Instead of counter += 1 , maybe use counter + 1 where you've used counter . 取而代之的counter += 1 ,也许还可以利用counter + 1你使用过counter

Alternatively: 或者:

for counter, file in ((i + 1, f) for i, f in enumerate(files)):
    ...

(Python 2.6 and later has some great stuff. Try to upgrade if you can.) (Python 2.6和更高版本有一些很棒的东西。如果可以,请尝试进行升级。)

You can make your own version of enumerate() : 您可以创建自己的enumerate()版本:

def enumerate_1based(iterable):
    for index, item in enumerate(iterable):
        yield index+1, item

Alternately, add a start argument, to make it work just like later versions of enumerate() . 或者,添加一个start参数,以使其像更高版本的enumerate()一样工作。

You can use zip() : 您可以使用zip()

>>> enums = zip(range(1, len(files) + 1), files)
>>> for index, val in enums:
    print index, val

I did this like this: 我这样做是这样的:

#Emulate enumerate() with start parameter (introduced in Python 2.6)
for i,v in (i+start,v for i,v in enumerate(seq)):
    //do stuff

Basically, this is the same, yet a self-contained construct. 基本上,这是相同的,但是一个独立的结构。

for (counter, files) in enumerate(files, start = 1)):
    # counter would start at 1
        //do stuff with file and counter
 for counter, item in enumerate(testlist):
    print(counter+1)
    print(item)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用索引向后枚举 python 中的列表(从结尾到开头) - How to enumerate over a list in python backwards with index(From the end to the beginning) Python-通过enumerate()迭代和替换列表索引 - Python - Iterating and Replacing List Index via enumerate() 使用python枚举列表中包含字典的列表 - Enumerate through list having dictionary in it using python 如何在带有 if 语句的枚举 for 循环中使用索引(python) - How to use the index in an enumerate for loop with if statement (python) 在 Python 中使用 enumerate() 将索引旋转回 `for` 循环中的第一个元素的更好方法 - Better way to rotate index back to first element in `for` loop with enumerate() in Python Python-枚举索引 - Python - index in enumerate 根据枚举列表Python从列表中获取第一个和最后一个元素 - Get first and last elements from list according to enumerate list Python Python如何使用枚举和列表跳过第一(即零)迭代? - Python how to skip the first (i.e. zero) iteration using enumerate and a list? python3,嵌套列表中的最小值,使用枚举保持索引 - python3, minimum in nested list keeping index using enumerate Python枚举列表设置开始索引但不增加结束计数 - Python enumerate list setting start index but without increasing end count
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM