将字典与其他字典进行比较,并仅选择存在于其他字典中的那些键

时间:2018-12-17 01:23:31

标签: python python-3.x performance dictionary for-loop

我有两个字典:

union = {'a':'gamma','b':'beta','d':'theta'}
packaged = {'a':'alpha','b':'gamma','c':'alpha'}

我想要:

packaged = {'a': 'alpha', 'b': 'gamma'}

因此,我只想从packaged中选择union中存在的键及其值。

我阅读了this个类似的问题,我正在做-

for k, v in list(packaged.items()):
    if k not in union.keys():
        del packaged[k]
print(packaged)

哪个给我想要的答案。

我的方法是最快/最有效的方法吗?如果不是,是否有任何更快/最有效的方法?

1 个答案:

答案 0 :(得分:4)

您可以使用字典理解。这将创建一个新的字典,但其复杂度与您的for循环相同:

d = {k: v for k, v in packaged.items() if k in union}

这是一些现实的基准测试,以及两个稍微更有效的变体:

union = {'a':'gamma','b':'beta','d':'theta', **dict.fromkeys(range(5000, 10000))}
packaged = {'a':'alpha', 'b':'gamma', 'c':'alpha', **dict.fromkeys(range(1, 15000))}

def dct_cmp(union, packaged):
    return {k: v for k, v in packaged.items() if k in union}

def dct_cmp_from_key(union, packaged):
    return {k: packaged[k] for k in packaged if k in union}

def dct_cmp_from_key_intersect(union, packaged):
    return {k: packaged[k] for k in set(packaged) & set(union)}

def loopy(union, packaged):
    for k, v in list(packaged.items()):
        if k not in union:
            del packaged[k]
    return packaged

assert dct_cmp(union, packaged.copy()) == loopy(union, packaged.copy())
assert dct_cmp(union, packaged.copy()) == dct_cmp_from_key(union, packaged.copy())
assert dct_cmp(union, packaged.copy()) == dct_cmp_from_key_intersect(union, packaged.copy())

%timeit dct_cmp(union, packaged.copy())                     # 1.94 ms
%timeit dct_cmp_from_key(union, packaged.copy())            # 1.8 ms
%timeit dct_cmp_from_key_intersect(union, packaged.copy())  # 1.8 ms
%timeit loopy(union, packaged.copy())                       # 2.75 ms