使用键列表从Python字典中收集值

时间:2015-05-15 02:37:36

标签: python function dictionary key

Dict = {'w1': 56, 'w2': 19, 'w3': 77, 'w4': 45, 'w5': 31}
myWords = ['w1','w4','w5']

OutputList=[]
for items in myWords:
    tmps = Dict[items]
    OutputList.append(tmps)

我的问题是,我们可以没有使用 for循环,从具有特定列表的字典中收集值(输出)(" myWord"?)

5 个答案:

答案 0 :(得分:19)

这是operator.itemgetter的用途:

>>> import operator
>>> Dict = {'w1': 56, 'w2': 19, 'w3': 77, 'w4': 45, 'w5': 31}
>>> myWords = ['w1','w4','w5']
>>> operator.itemgetter(*myWords)(Dict)
[56, 45, 31]

答案 1 :(得分:8)

您可以使用列表理解

OutputList = [Dict[x] for x in myWords]

答案 2 :(得分:5)

以下是几种不同方法的基准:

from __future__ import print_function
import timeit
from operator import itemgetter

def f1(d, l):
    '''map'''
    return list(map(d.get, l))

def f2(d, l):
    '''itemgetter'''
    return itemgetter(*l)(d)

def f3(d, l):
    '''list comprehension'''
    return [d[k] for k in l]

def f4(d, l):
    '''WRONG, but map and filter'''
    return list(map(lambda k: d[k], filter(d.get, l)))

def f5(d, l):
    '''simple for loop'''
    rtr=[]
    for e in l:
        rtr.append(d[e])
    return rtr  

def f6(d, l):
    '''CORRECTED map, filter '''    
    return list(map(lambda k: d[k], filter(d.__contains__, l))) 

if __name__ == '__main__':
    s=10000000
    d={'W{}'.format(k):k for k in range(s)} 
    l=['W{}'.format(x) for x in range(0,s,4)]

    times=[]                
    for f in (f1,f2,f3,f4,f5,f6):
        times.append((f.__doc__, timeit.timeit('f(d,l)', setup="from __main__ import f, d, l", number=10)))

    for e in sorted(times, key=itemgetter(1)):
         print('{:30}{:10.3f} seconds'.format(*e))

对于Python 2.7,打印:

itemgetter                         4.109 seconds
list comprehension                 4.467 seconds
map                                5.450 seconds
simple for loop                    6.132 seconds
CORRECTED map, filter             11.283 seconds
WRONG, but map and filter         11.852 seconds

Python 3.4:

itemgetter                         5.196 seconds
list comprehension                 5.224 seconds
map                                5.923 seconds
simple for loop                    6.548 seconds
WRONG, but map and filter          9.080 seconds
CORRECTED map, filter              9.931 seconds

PyPy:

list comprehension                 4.450 seconds
map                                4.718 seconds
simple for loop                    5.962 seconds
itemgetter                         7.952 seconds
WRONG, but map and filter          8.962 seconds
CORRECTED map, filter              9.909 seconds

你可以看到,即使是与OP所说的相似大小的字典(1,000,000个元素),也可以看到一个简单的' for'循环与更高级的方法竞争。列表理解非常有竞争力。

你还可以看到看起来很花哨的东西并不是那么好。

过早优化是所有邪恶的根源

答案 3 :(得分:3)

或使用地图,如果myWords包含字典Dict的键,则使用

OutputList = map(Dict.get, myWords)

答案 4 :(得分:2)

l = ['a', 'b', 'c'] 
d = { 'a': 1, 'b': 2}
result = map(lambda x: d[x], filter(d.get, l))
print result #[1, 2]