计算列表差异时,如何避免出现“ TypeError:无法散列的类型:'dict'”?

问题描述

我正在使用Python 3.8。我有两个列表,每个元素都是字典...

>>> existing_dicts = [{"id": 1},{"id": 2}]
>>> cur_dicts = [{"id": 2}]

我想找到不再存在于“ cur_dicts”中的,原本存在于“ existing_dicts”中的字典。因此,在上面的示例中,

{"id": 1}

是我想要的结果,因为它在“ existing_dicts”中,而不在“ cur_dicts”中。我尝试了以下方法来找到区别...

>>> deleted_dicts = list(set(existing_dicts) - set(cur_dicts))
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
TypeError: unhashable type: 'dict'

有什么更好的方法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)