Python 如何枚举从 1 开始的一系列数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3303608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enumerate a range of numbers starting at 1
提问by JeffTaggary
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 python2.5:
使用python2.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 中获得所需结果的方法吗?
Thanks,
谢谢,
Jeff
杰夫
采纳答案by Mark Byers
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 startparameter 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 izipinstead.
如果要创建生成器而不是列表,则可以改用izip。
回答by Chris B.
h = [(i + 1, x) for i, x in enumerate(xrange(2000, 2005))]
h = [(i + 1, x) for i, x in enumerate(xrange(2000, 2005))]
回答by Eli Courtwright
>>> 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))
回答by Jochen Ritzel
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))工作。
回答by Duncan
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
回答by Duncan
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. Alternatively, this oneliner may be more elegant and possibly faster, but breaks other uses of enumerate:
请注意,这不会使用没有 start 参数的 enumerate 破坏代码。或者,这个 oneliner 可能更优雅,可能更快,但打破了 enumerate 的其他用途:
enumerate = ((index+1, item) for index, item)
enumerate = ((index+1, item) for index, item)
The latter was pure nonsense. @Duncan got the wrapper right.
后者纯属无稽之谈。@Duncan 正确包装。
回答by JAB
>>> list(enumerate(range(1999, 2005)))[1:]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]
回答by Alex Martelli
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 zipin 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))。
回答by Nas Banov
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)]? If you won't function, no problem either:
好吧,我觉得这里有点愚蠢......有什么理由不只是用类似的东西来做[(a+1,b) for (a,b) in enumerate(r)]?如果您无法运行,也没有问题:
>>> 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)]
回答by dhackner
Just to put this here for posterity sake, in 2.6 the "start" parameter was added to enumerate like so:
只是为了后代的缘故把它放在这里,在 2.6 中添加了“start”参数来枚举,如下所示:
enumerate(sequence, start=1)
enumerate(sequence, start=1)

