我假设完整的测试要求 exception 或 excluded 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_dict、equal_dict、partial_dict、identical_dict 和not_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 用例,即字典始终具有相同的键,则基于all() 的解决方案将更快,因为它们将具有显式短路,并且它们可以通过以下方式转换变化:
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)