小编Abe*_*ark的帖子

Python 3.x:如何比较包含顺序无关紧要的字典的两个列表

我有可能包含其他字典或列表的嵌套字典。我需要能够比较这些字典的列表(或集合,真的)以表明它们是相等的。

列表的顺序不统一。通常,我会将列表转换为集合,但这是不可能的,因为有些值也是字典。

a = {'color': 'red'}
b = {'shape': 'triangle'}
c = {'children': [{'color': 'red'}, {'age': 8},]}

test_a = [a, b, c] 
test_b = [b, c, a]

print(test_a == test_b)  # False
print(set(test_a) == set(test_b))  # TypeError: unhashable type: 'dict'
Run Code Online (Sandbox Code Playgroud)

有没有一种很好的方法来解决这个问题以显示test_atest_b?

python python-3.x

5
推荐指数
1
解决办法
2068
查看次数

标签 统计

python ×1

python-3.x ×1