如何在另一个词典中区分2个词典?

时间:2018-02-02 07:39:22

标签: python python-3.x dictionary

我有2个词典本应完全相同但有一个小的差异,大约0.0035%,但考虑到我的数据量,仍然是相当多的元素。

我想要的是在第3个词典中区分2个词典,例如:

mydict = {
    'a': [[2,4], [5,6]],
    'b': [[1,1], [1,7,9], [6,2,3]],
    'c': [['a'], [4,5]],
}

second_dict = {'a': [[2,4]],
        'b': [[1,1], [1,7,9], [6,2,3]],

    }

让我们假设mydict是所有元素的基本事实,而second_dict是缺少一些元素,diff_dict应该是:

 diff_dict = {'a': [[5,6]],
            'c': [['a'], [4,5]],    
        }

2 个答案:

答案 0 :(得分:2)

>>> diff_dict = {k:[x for x in v if x not in second_dict.get(k, [])] for k,v in mydict.items()}
>>> diff_dict
{'c': [['a'], [4, 5]], 'a': [[5, 6]], 'b': []}
>>> diff_dict = {k:v for k,v in diff_dict.items() if v}
>>> diff_dict
{'c': [['a'], [4, 5]], 'a': [[5, 6]]}

我建议将mydictsecond_dict中的值设置为而不是列表,以提高效率和更清晰的代码。

答案 1 :(得分:0)

这可行,但效率不高,因为它会为second_dict.get()中的每个条目重复mydict

mydict = {
    'a': [[2,4], [5,6]],
    'b': [[1,1], [1,7,9], [6,2,3]],
    'c': [['a'], [4,5]],
}
second_dict = {
    'a': [[2,4]],
    'b': [[1,1], [1,7,9], [6,2,3]],
}
diff_dict = {
    k: [v for v in vals if v not in second_dict.get(k, [])]
    for k, vals in mydict.items()
}
# only keep non-empty lists
diff_dict = {k: v for k, v in diff_dict.items() if v}
diff_dict
# {'a': [[5, 6]], 'c': [['a'], [4, 5]]}

这更难阅读,但效率更高:

diff_dict = {
    k: (
        [v for v in vals if v not in second_dict[k]] if k in second_dict 
        else vals
    ) 
    for k, vals in mydict.items()
}
diff_dict = {k: v for k, v in diff_dict.items() if v}

如果您愿意将项目保存在元组集中而不是列表列表中,那么您可以更轻松有效地完成这些工作:

mydict = {
    'a': {(2,4), (5,6)},
    'b': {(1,1), (1,7,9), (6,2,3)},
    'c': {('a',), (4,5)},
}
second_dict = {
    'a': {(2,4)},
    'b': {(1,1), (1,7,9), (6,2,3)},
}
diff_dict = {key: full - second_dict.get(key, set()) for key, full in mydict.items()}
diff_dict
# {'a': set([(5, 6)]), 'c': set([(4, 5), ('a',)]), 'b': set([])}