简体   繁体   English

如何枚举从 1 开始的一系列数字

[英]How to enumerate a range of numbers starting at 1

I am using Python 2.5, I want an enumeration like so (starting at 1 instead of 0):我正在使用 Python 2.5,我想要一个像这样的枚举(从 1 而不是 0 开始):

[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

I know in Python 2.6 you can do: h = enumerate(range(2000, 2005), 1) to give the above result but in python2.5 you cannot...我知道在 Python 2.6 中你可以这样做: h = enumerate(range(2000, 2005), 1) 给出上述结果,但在 python2.5 中你不能......

Using Python 2.5:使用 Python 2.5:

>>> h = enumerate(range(2000, 2005))
>>> [x for x in h]
[(0, 2000), (1, 2001), (2, 2002), (3, 2003), (4, 2004)]

Does anyone know a way to get that desired result in Python 2.5?有谁知道在 Python 2.5 中获得所需结果的方法吗?

As you already mentioned, this is straightforward to do in Python 2.6 or newer:正如您已经提到的,这在 Python 2.6 或更高版本中很容易做到:

enumerate(range(2000, 2005), 1)

Python 2.5 and older do not support the start parameter so instead you could create two range objects and zip them: Python 2.5 及更早版本不支持start参数,因此您可以创建两个范围对象并压缩它们:

r = xrange(2000, 2005)
r2 = xrange(1, len(r) + 1)
h = zip(r2, r)
print h

Result:结果:

[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

If you want to create a generator instead of a list then you can use izip instead.如果要创建生成器而不是列表,则可以改用izip

只是为了后代的缘故把它放在这里,在 2.6 中添加了“start”参数来枚举,如下所示:

enumerate(sequence, start=1)

Python 3蟒蛇 3

Official Python documentation: enumerate(iterable, start=0) Python 官方文档: enumerate(iterable, start=0)

You don't need to write your own generator as other answers here suggest.您不需要像这里的其他答案所建议的那样编写自己的生成器 The built-in Python standard library already contains a function that does exactly what you want:内置的Python 标准库已经包含一个函数,它完全符合你的要求:

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

The built-in function is equivalent to this:内置函数等价于:

def enumerate(sequence, start=0):
  n = start
  for elem in sequence:
    yield n, elem
    n += 1

Easy, just define your own function that does what you want:很简单,只需定义您自己的函数来执行您想要的操作:

def enum(seq, start=0):
    for i, x in enumerate(seq):
        yield i+start, x

Simplest way to do in Python 2.5 exactly what you ask about:在 Python 2.5 中最简单的方法就是你所问的:

import itertools as it

... it.izip(it.count(1), xrange(2000, 2005)) ...

If you want a list, as you appear to, use zip in lieu of it.izip .如果你想要一个列表,就像你看起来的那样,使用zip代替it.izip

(BTW, as a general rule, the best way to make a list out of a generator or any other iterable X is not [x for x in X] , but rather list(X) ). (顺便说一句,作为一般规则,从生成器或任何其他可迭代 X 中创建列表的最佳方法不是[x for x in X] ,而是list(X) )。

from itertools import count, izip

def enumerate(L, n=0):
    return izip( count(n), L)

# if 2.5 has no count
def count(n=0):
    while True:
        yield n
        n+=1

Now h = list(enumerate(xrange(2000, 2005), 1)) works.现在h = list(enumerate(xrange(2000, 2005), 1))有效。

enumerate is trivial, and so is re-implementing it to accept a start: enumerate 是微不足道的,因此重新实现它以接受开始:

def enumerate(iterable, start = 0):
    n = start
    for i in iterable:
        yield n, i
        n += 1

Note that this doesn't break code using enumerate without start argument.请注意,这不会使用没有 start 参数的 enumerate 破坏代码。 Alternatively, this oneliner may be more elegant and possibly faster, but breaks other uses of enumerate:或者,这个 oneliner 可能更优雅,可能更快,但打破了 enumerate 的其他用途:

 enumerate = ((index+1, item) for index, item)

The latter was pure nonsense.后者纯属无稽之谈。 @Duncan got the wrapper right. @Duncan 正确包装。

>>> list(enumerate(range(1999, 2005)))[1:]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

h = [(i + 1, x) for i, x in enumerate(xrange(2000, 2005))]

Ok, I feel a bit stupid here... what's the reason not to just do it with something like好吧,我觉得这里有点愚蠢......有什么理由不只是用类似的东西来做
[(a+1,b) for (a,b) in enumerate(r)] ? [(a+1,b) for (a,b) in enumerate(r)] ? If you won't function, no problem either:如果您无法运行,也没有问题:

>>> r = range(2000, 2005)
>>> [(a+1,b) for (a,b) in enumerate(r)]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

>>> enumerate1 = lambda r:((a+1,b) for (a,b) in enumerate(r)) 

>>> list(enumerate1(range(2000,2005)))   # note - generator just like original enumerate()
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]
>>> h = enumerate(range(2000, 2005))
>>> [(tup[0]+1, tup[1]) for tup in h]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]

Since this is somewhat verbose, I'd recommend writing your own function to generalize it:由于这有点冗长,我建议您编写自己的函数来概括它:

def enumerate_at(xs, start):
    return ((tup[0]+start, tup[1]) for tup in enumerate(xs))

I don't know how these posts could possibly be made more complicated then the following:我不知道这些帖子怎么可能比以下内容更复杂:

# Just pass the start argument to enumerate ...
for i,word in enumerate(allWords, 1):
    word2idx[word]=i
    idx2word[i]=word

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM