在 Python 中删除列表中的重复字典 [英] Remove duplicate dict in list in Python

查看:64
本文介绍了在 Python 中删除列表中的重复字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典列表,我想删除具有相同键值对的字典.

I have a list of dicts, and I'd like to remove the dicts with identical key and value pairs.

对于这个列表:[{'a': 123}, {'b': 123}, {'a': 123}]

我想返回这个:[{'a': 123}, {'b': 123}]

另一个例子:

对于这个列表:[{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}, {'a': 123, 'b':1234}]

我想返回这个:[{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}]

推荐答案

试试这个:

[dict(t) for t in {tuple(d.items()) for d in l}]

策略是将字典列表转换为元组列表,其中元组包含字典的项目.由于元组可以散列,您可以使用 set 删除重复项(此处使用 set comprehension,较旧的 Python 替代方法是 set(tuple(d.items()) for d in l)) ,然后用 dict 从元组重新创建字典.

The strategy is to convert the list of dictionaries to a list of tuples where the tuples contain the items of the dictionary. Since the tuples can be hashed, you can remove duplicates using set (using a set comprehension here, older python alternative would be set(tuple(d.items()) for d in l)) and, after that, re-create the dictionaries from tuples with dict.

哪里:

  • l 是原始列表
  • d 是列表中的字典之一
  • t 是从字典中创建的元组之一
  • l is the original list
  • d is one of the dictionaries in the list
  • t is one of the tuples created from a dictionary

如果您想保留顺序,上面的单行代码将不起作用,因为 set 不会这样做.但是,通过几行代码,您也可以做到:

If you want to preserve ordering, the one-liner above won't work since set won't do that. However, with a few lines of code, you can also do that:

l = [{'a': 123, 'b': 1234},
        {'a': 3222, 'b': 1234},
        {'a': 123, 'b': 1234}]

seen = set()
new_l = []
for d in l:
    t = tuple(d.items())
    if t not in seen:
        seen.add(t)
        new_l.append(d)

print new_l

示例输出:

[{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}]

注意:正如@a​​lexis 指出的那样,两个具有相同键和值的字典可能不会产生相同的元组.如果他们经历了不同的添加/删除密钥历史记录,就会发生这种情况.如果您的问题就是这种情况,请考虑按照他的建议对 d.items() 进行排序.

Note: As pointed out by @alexis it might happen that two dictionaries with the same keys and values, don't result in the same tuple. That could happen if they go through a different adding/removing keys history. If that's the case for your problem, then consider sorting d.items() as he suggests.

这篇关于在 Python 中删除列表中的重复字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆