已经回答了,但由于一些人提到反转字典,下面是如何在一行中做到这一点(假设1:1映射)和一些各种性能数据:
python 2.6:
reversedict = dict([(value, key) for key, value in mydict.iteritems()])
+ 2.7:
reversedict = {value:key for key, value in mydict.iteritems()}
如果你认为不是1:1,你仍然可以用几行创建一个合理的反向映射:
reversedict = defaultdict(list)
[reversedict[value].append(key) for key, value in mydict.iteritems()]
这有多慢:比简单的搜索慢,但远没有你想象的那么慢——在一个“直接”100000条目的字典上,“快速”搜索(即查找键前面的值)比反转整个字典快10倍左右,而“缓慢”搜索(接近结尾)大约快4-5倍。所以最多查找10次,就能收回成本。
第二个版本(每个项目都有列表)大约是简单版本的2.5倍。
largedict = dict((x,x) for x in range(100000))
# Should be slow, has to search 90000 entries before it finds it
In [26]: %timeit largedict.keys()[largedict.values().index(90000)]
100 loops, best of 3: 4.81 ms per loop
# Should be fast, has to only search 9 entries to find it.
In [27]: %timeit largedict.keys()[largedict.values().index(9)]
100 loops, best of 3: 2.94 ms per loop
# How about using iterkeys() instead of keys()?
# These are faster, because you don't have to create the entire keys array.
# You DO have to create the entire values array - more on that later.
In [31]: %timeit islice(largedict.iterkeys(), largedict.values().index(90000))
100 loops, best of 3: 3.38 ms per loop
In [32]: %timeit islice(largedict.iterkeys(), largedict.values().index(9))
1000 loops, best of 3: 1.48 ms per loop
In [24]: %timeit reversedict = dict([(value, key) for key, value in largedict.iteritems()])
10 loops, best of 3: 22.9 ms per loop
In [23]: %%timeit
....: reversedict = defaultdict(list)
....: [reversedict[value].append(key) for key, value in largedict.iteritems()]
....:
10 loops, best of 3: 53.6 ms per loop
过滤器也有一些有趣的结果。理论上,filter应该更快,因为我们可以使用itervalues(),而且可能不需要创建/遍历整个值列表。在实践中,结果是……奇怪的……
In [72]: %%timeit
....: myf = ifilter(lambda x: x[1] == 90000, largedict.iteritems())
....: myf.next()[0]
....:
100 loops, best of 3: 15.1 ms per loop
In [73]: %%timeit
....: myf = ifilter(lambda x: x[1] == 9, largedict.iteritems())
....: myf.next()[0]
....:
100000 loops, best of 3: 2.36 us per loop
因此,对于小偏移量,它比以前的任何版本都要快得多(2.36 *u*S vs.以前的情况下至少1.48 *m*S)。然而,对于接近列表末尾的大偏移量,它会显着变慢(15.1ms vs.相同的1.48mS)。以我之见,在低端产品上节省下来的少量成本,在高端产品上的成本是不值的。