How to enumerate a range of numbers starting at 1

后端 未结 12 1137
谎友^
谎友^ 2020-11-29 23:42

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)]

12条回答
  •  温柔的废话
    2020-11-29 23:53

    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)).

提交回复
热议问题