问题:如何枚举从1开始的数字范围
我正在使用Python 2.5,我想要这样的枚举(从1而不是0开始):
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]
我知道在python 2.6中可以执行以下操作:h = enumerate(range(2000,2005),1)给出上述结果,但是在python2.5中您不能…
使用python2.5:
>>> h = enumerate(range(2000, 2005))
>>> [x for x in h]
[(0, 2000), (1, 2001), (2, 2002), (3, 2003), (4, 2004)]
有谁知道在python 2.5中获得理想结果的方法?
谢谢,
杰夫
I am using Python 2.5, I want an enumeration like so (starting at 1 instead of 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…
Using 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?
Thanks,
Jeff
回答 0
正如您已经提到的,在Python 2.6或更高版本中,这很容易做到:
enumerate(range(2000, 2005), 1)
Python 2.5及更早版本不支持该start
参数,因此您可以创建两个范围对象并将其压缩:
r = xrange(2000, 2005)
r2 = xrange(1, len(r) + 1)
h = zip(r2, r)
print h
结果:
[(1,2000),(2,2001),(3,2002),(4,2003),(5,2004)]
如果要创建生成器而不是列表,则可以使用izip。
As you already mentioned, this is straightforward to do in Python 2.6 or newer:
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:
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.
回答 1
只是为了后代而已,在2.6中添加了“ start”参数来进行枚举,如下所示:
enumerate(sequence, start=1)
Just to put this here for posterity sake, in 2.6 the “start” parameter was added to enumerate like so:
enumerate(sequence, start=1)
回答 2
简单,只需定义您自己的函数即可执行您想要的操作:
def enum(seq, start=0):
for i, x in enumerate(seq):
yield i+start, x
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
回答 3
Python 3
官方文件: enumerate(iterable, start=0)
所以您可以这样使用它:
>>> 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')]
Python 3
Official documentation: enumerate(iterable, start=0)
So you would use it like this:
>>> 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')]
回答 4
在Python 2.5中执行查询的最简单方法就是:
import itertools as it
... it.izip(it.count(1), xrange(2000, 2005)) ...
如果您想要一个列表(看起来像),请使用zip
代替it.izip
。
(顺便说一句,顺便说一句,从生成器或任何其他可迭代X生成列表的最佳方法不是 [x for x in X]
,而是list(X)
。)
Simplest way to do in Python 2.5 exactly what you ask about:
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
.
(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)
).
回答 5
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
现在h = list(enumerate(xrange(2000, 2005), 1))
可以使用了。
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.
回答 6
枚举是微不足道的,因此重新实现它以接受一个开始:
def enumerate(iterable, start = 0):
n = start
for i in iterable:
yield n, i
n += 1
请注意,这没有使用没有开始参数的枚举不会破坏代码。另外,此oneliner可能更优雅,甚至可能更快,但破坏了枚举的其他用途:
enumerate = ((index+1, item) for index, item)
后者纯粹是胡说八道。@邓肯正确的包装。
enumerate is trivial, and so is re-implementing it to accept a start:
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:
enumerate = ((index+1, item) for index, item)
The latter was pure nonsense. @Duncan got the wrapper right.
回答 7
>>> list(enumerate(range(1999, 2005)))[1:]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]
>>> list(enumerate(range(1999, 2005)))[1:]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]
回答 8
h = [(i + 1, x) for i, x in enumerate(xrange(2000, 2005))]
h = [(i + 1, x) for i, x in enumerate(xrange(2000, 2005))]
回答 9
好的,我在这里有点愚蠢……为什么不只做类似的事情
[(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)]
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:
>>> 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)]
回答 10
>>> h = enumerate(range(2000, 2005))
>>> [(tup[0]+1, tup[1]) for tup in h]
[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]
由于这有点冗长,因此建议您编写自己的函数以对其进行概括:
def enumerate_at(xs, start):
return ((tup[0]+start, tup[1]) for tup in enumerate(xs))
>>> 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))
回答 11
我不知道这些帖子如何变得比以下内容更复杂:
# Just pass the start argument to enumerate ...
for i,word in enumerate(allWords, 1):
word2idx[word]=i
idx2word[i]=word
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