相关疑难解决方法(0)

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

我使用的是Python 2.5,我想要一个像这样的枚举(从1开始而不是0):

[(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)]
Run Code Online (Sandbox Code Playgroud)

我知道在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)]
Run Code Online (Sandbox Code Playgroud)

有谁知道在python 2.5中获得所需结果的方法?

谢谢,

杰夫

python enums

130
推荐指数
8
解决办法
11万
查看次数

一个班轮:从列表创建一个字典,索引作为键

我想在一行中创建给定列表中的字典.字典的键将是索引,值将是列表的元素.像这样的东西:

a = [51,27,13,56]         #given list

d = one-line-statement    #one line statement to create dictionary

print(d)
Run Code Online (Sandbox Code Playgroud)

输出:

{0:51, 1:27, 2:13, 3:56}
Run Code Online (Sandbox Code Playgroud)

我没有任何具体的要求,为什么我要一个线.我只是在探索python,并想知道这是否可行.

python dictionary list python-3.x

87
推荐指数
4
解决办法
8万
查看次数

标签 统计

python ×2

dictionary ×1

enums ×1

list ×1

python-3.x ×1