我使用的是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中获得所需结果的方法?
谢谢,
杰夫
我想在一行中创建给定列表中的字典.字典的键将是索引,值将是列表的元素.像这样的东西:
a = [51,27,13,56]         #given list
d = one-line-statement    #one line statement to create dictionary
print(d)
输出:
{0:51, 1:27, 2:13, 3:56}
我没有任何具体的要求,为什么我要一个线.我只是在探索python,并想知道这是否可行.