Python:如何比较两个字典列表

标签 python

亲们, 相对于 python 的 n00b,试图找出两个字典列表的差异。

如果这些只是常规列表,我可以创建集合,然后执行“-”/相交操作。

但是,集合操作不适用于字典列表:

>>> l = []
>>> pool1 = {}
>>> l.append(pool1)
>>> s = set(l)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

最佳答案

你需要一个“可散列”的字典。

items() 属性是一个元组列表。将其设为 tuple(),您就拥有了字典的可散列版本。

tuple( sorted( some_dict.items() ) )

关于Python:如何比较两个字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5783745/

相关文章:

python - Intellij 找不到通过 pip 安装的 Python 模块

python - TypeError : sequence item 0: expected string, int 找到

python - 限制更新方法只能修改请求用户自己的数据?

python - R apply在Python中的匹配速度

python - 如何抑制来自 Tensorflow 的所有签名警告?

python - 打印一个奇特的字符串并在打印后更新它以模拟 GUI

Python 命名约定指南

python - 如何在这里进行广义特征分解?

python - Selenium pdf自动下载不起作用

python - 输入名称的 Django 表单集错误