python:带有列表索引的字典理解

met*_*oia 0 python dictionary python-2.7

我想用一个包含列表中值的索引位置的键创建字典.我正在使用python 2.7.考虑我的尝试:

LL = ["this","is","a","sample","list"]
LL_lookup = {LL.index(l):l for (LL.index(l), l) in LL}

# desired output
print LL_lookup[1]
>> is
Run Code Online (Sandbox Code Playgroud)

我认识到在这个例子中不需要字典 - LL[1]会产生相同的结果.尽管如此,我们可以想象一种情况,其中1)字典是优选的,给出更复杂的例子,和b)字典查找可以通过大量迭代产生边际性能增益.

jam*_*lak 11

>>> LL = ["this","is","a","sample","list"]
>>> dict(enumerate(LL))
{0: 'this', 1: 'is', 2: 'a', 3: 'sample', 4: 'list'}
Run Code Online (Sandbox Code Playgroud)