fix: compare_lists_of_dicts的bug
This commit is contained in:
parent
bf5d62e99d
commit
a1e2f12df7
|
@ -220,9 +220,11 @@ def compare_lists_of_dicts(list1, list2, ignore_order=False):
|
|||
"""比较两个列表,这里的列表包含字典(对象)"""
|
||||
if ignore_order:
|
||||
# 转换列表中的字典为元组列表,然后排序进行比较
|
||||
sorted_list1 = sorted((tuple(sorted(d.items())) for d in list1))
|
||||
sorted_list2 = sorted((tuple(sorted(d.items())) for d in list2))
|
||||
return sorted_list1 == sorted_list2
|
||||
if list1 and isinstance(list1[0], dict):
|
||||
sorted_list1 = sorted((tuple(sorted(d.items())) for d in list1))
|
||||
sorted_list2 = sorted((tuple(sorted(d.items())) for d in list2))
|
||||
return sorted_list1 == sorted_list2
|
||||
return list1 == list2
|
||||
else:
|
||||
# 按顺序比较列表中的字典
|
||||
return all(compare_dicts(obj1, obj2) for obj1, obj2 in zip(list1, list2))
|
||||
|
@ -230,6 +232,7 @@ def compare_lists_of_dicts(list1, list2, ignore_order=False):
|
|||
|
||||
def compare_values(val1, val2, ignore_order=False):
|
||||
"""通用比较函数,也可以处理字典和列表。"""
|
||||
print(val1, val2)
|
||||
if isinstance(val1, list) and isinstance(val2, list):
|
||||
# 假设这里我们关心列表中对象的顺序
|
||||
return compare_lists_of_dicts(val1, val2, ignore_order)
|
||||
|
|
Loading…
Reference in New Issue