python-3.x 如何检查除某些字段外两个字典是否相等?

vkc1a9a2  于 2023-03-04  发布在  Python
关注(0)|答案(8)|浏览(125)

有两条格言:旧的和更新的。我想检查他们是否相等,除了statuslatitudelongitude键。

assert old_dict['status'] != updated_dict['status']
assert old_dict['latitude'] != updated_dict['latitude']
assert old_dict['longitude'] != updated_dict['longitude']

for field in ('status', 'latitude', 'longitude'):
    updated_dict.pop(field)
    old_dict.pop(field)

assert old_dict == updated_dict

更像Python的方法是什么?

neekobn8

neekobn81#

你可以Assert两个dict的项之间的对称差是三个键:

assert {k for k, _ in old_dict.items() ^ updated_dict.items()} == {'status', 'latitude', 'longitude'}
mum43rcc

mum43rcc2#

有点非正统的建议,但听我说完:

differing = {"status", "latitude", "longitude"}
assert all(
    (old_dict[key] != updated_dict[key]) == (key in differing)
    for key in old_dict
)

对于每个key,我们Assert当且仅当键是不同的键之一时,值不同。

ee7vknir

ee7vknir3#

您可以通过字典解析过滤两个字典,然后检查是否相等:

def compare_dicts(d1, d2, exc_keys):
    dct1 = {k: v for k, v in d1.items() if k not in exc_keys}
    dct2 = {k: v for k, v in d2.items() if k not in exc_keys}
    return dct1 == dct2

assert compare_dicts(old_dict, updated_dict, {'status', 'latitude', 'longitude'})
nwlls2ji

nwlls2ji4#

我假设完整的测试要求 exceptionexcluded dict键必须不同,并且这两个字典可能不具有所有共同的键。
一些测试用例可编写如下:

import string
import random

random.seed(0)

keys = list(string.ascii_letters)
excluded = 'r', 'm', 'e'

# the original dict
base_dict = {key: random.randint(1, 100) for key in keys}

# some keys, different from excluded are different
unequal_dict = {key: (val if key not in ('q') else random.randint(1, 100)) for key, val in base_dict.items()}

# only the excluded keys are different
equal_dict = {key: (val if key not in excluded else random.randint(1, 100)) for key, val in base_dict.items()}

# only some of the excluded keys are different
partial_dict = {key: (val if key not in excluded[1:] else random.randint(1, 100)) for key, val in base_dict.items()}

# a copy of the base dict
identical_dict = base_dict.copy()

# one more key is added
not_same_keys_dict = base_dict.copy()
not_same_keys_dict['aa'] = 1

其中old_dict基本上是base_dict,而unequal_dictequal_dictpartial_dictidentical_dictnot_same_keys_dict涵盖了不同的极端情况。
然后,我们定义一些辅助函数来同时测试不同的输入。

def multi_test(func, many_args):
    return [func(*args) for args in many_args]

many_args = (
    (base_dict, unequal_dict, updated),
    (base_dict, equal_dict, updated),
    (base_dict, partial_dict, updated),
    (base_dict, identical_dict, updated),
    (base_dict, not_same_keys_dict, updated))

函数化后的原始代码如下所示:

import copy

def dicts_equal_except_orig(dict1, dict2, excluded):
    dict1 = dict1.copy()
    dict2 = dict2.copy()
    result = True
    for key in excluded:
        result = result and (dict1[key] != dict2[key])
        dict1.pop(key)
        dict2.pop(key)
    result = result and (dict1 == dict2)
    return result

print(multi_test(dicts_equal_except_orig, many_args))
# [False, True, False, False, False]

%timeit multi_test(dicts_equal_except_orig, many_args)
# 13.1 µs ± 183 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

假设要比较的字典有一些不相同的键,那么这就是生成的测试所能达到的最快速度。所有其他方法都要慢得多,尽管可能更干净,在某些情况下甚至可能更快,例如,当要排除的键的数量很大时,等等。另外,如果不需要not_same_key用例,即,Dict总是具有相同的密钥,则基于X1 M9 N1 X的解决方案将更快,因为它们将具有显式短路,并且它们可以通过改变以下各项来转换:

keys = dict1.keys() | dict2.keys()

例如

keys = dict1.keys()

并删除其他健全性检查,如if key in dict1 and key in dict2
为了完整起见,我报告了我测试的所有其他选项:

使用显式测试的自己的解决方案

def dicts_equal_except(dict1, dict2, excluded):
    keys = dict1.keys() | dict2.keys()
    return all(
        (dict1[key] != dict2[key] if key in excluded else dict1[key] == dict2[key])
        if key in dict1 and key in dict2 else False
        for key in keys)

print(multi_test(dicts_equal_except, many_args))
# [False, True, False, False, False]

%timeit multi_test(dicts_equal_except, many_args)
# 28.3 µs ± 186 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@blhsing溶液的功能化

def check_dict_except(dict1, dict2, excluded):
    return {k for k, _ in dict1.items() ^ dict2.items()} == set(excluded)

print(multi_test(check_dict_except, many_args))
# [False, True, False, False, False]

%timeit multi_test(check_dict_except, many_args)
# 30.8 µs ± 498 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@L3viathan解决方案的##变体

def dicts_equal_all(dict1, dict2, excluded):
    keys = dict1.keys() | dict2.keys()
    return all((dict1[key] == dict2[key]) ^ (key in excluded) for key in keys)

print(multi_test(dicts_equal_all, many_args))
# [False, True, False, False, False]

%timeit multi_test(dicts_equal_all, many_args)
# 29.7 µs ± 316 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

以及

def dicts_equal_all2(dict1, dict2, excluded):
    keys = dict1.keys() | dict2.keys()
    return all((dict1[key] != dict2[key]) == (key in excluded) for key in keys)

print(multi_test(dicts_equal_all2, many_args))
# [False, True, False, False, False]

%timeit multi_test(dicts_equal_all2, many_args)
# 29.9 µs ± 435 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

改编自@jpp答案:

def compare_dicts(dict1, dict2, excluded):
    filter_dict1 = {key: val for key, val in dict1.items() if key not in excluded}
    filter_dict2 = {key: val for key, val in dict2.items() if key not in excluded}
    excluded_dict1 = {key: dict1[key] for key in excluded if key in dict1}
    excluded_dict2 = {key: dict2[key] for key in excluded if key in dict2}
    return filter_dict1 == filter_dict2 and all(dict1[key] != dict2[key] if key in dict1 and key in dict2 else False for key in excluded)

print(multi_test(compare_dicts, many_args))
# [False, True, False, False, False]

%timeit multi_test(compare_dicts, many_args)
# 57.5 µs ± 960 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
pprl5pva

pprl5pva5#

试试这个:

old_dict.keys() == updated_dict.keys()

如果update_dict密钥是update_dict密钥的子集,则它将为真。

6rqinv9w

6rqinv9w6#

几行,但应该相当快,因为它不会改变要比较的dict对的结构。

EXC = {"status", "latitude", "longitude"}
saved = {}

for key in EXC:
   saved[key], updated_dict[key] = updated_dict[key], old_dict[key]
cmp = old_dict == updated_dict
for key in EXC:
   old_dict[key], updated_dict[key] = updated_dict[key], saved[key]
pcww981p

pcww981p7#

不如

ignore = {'status', 'latitude', 'longitude'}
equal = all([old_value == new[key]
             for key, old_value in old.items()
             if key not in ignore])

这将在dict上迭代一次(我不认为有办法绕过它)。

kqhtkvqz

kqhtkvqz8#

有一个名为deepdiff的库

from deepdiff import DeepDiff
expected = {'couponCode': '1234', 'usedOnOrder': ['1'], 'usedOnOwners': [], 'sth':{'sth':'sth'}}
actual = {'couponCode': '1234', 'usedOnOrder': ['1'], 'usedOnOwners': [], 'usageCount': 1}
DeepDiff(expected, actual)

它会将所有差异输出为字典,如下所示

{'dictionary_item_added': [root['usageCount']],
 'dictionary_item_removed': [root['sth']]}

然后,可以在传入之前删除所需的字段。如果输入字典相同,则将返回空字典
下面是您案例示例

from deepdiff import DeepDiff
expected = {'status': '1234', 'latitude': ['1'], 'longitude': [], 'sth':{'sth':'sth'}}
actual = {'status': '123', 'latitude': ['2'], 'longitude': [], 'sth':{'sth':'sth'}}
ignored_fields = ['status', 'latitude', 'longitude']

dict_filter = lambda x: {k:v for k,v in x.items() if k not in ignored_fields}

diff = DeepDiff(dict_filter(expected), dict_filter(actual))
assert not diff, diff

相关问题