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

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

问题描述



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



d想返回: [{'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 )for t in set([tuple(d.items())for d in l])] 

策略是将字典列表转换为元组列表,其中元组包含字典的项目。由于可以使用元组进行散列,因此您可以使用 set 删除重复项,之后,使用 dict

其中:




  • l 是原始列表

  • d 是列表中的其中一个字典

  • t 是从字典创建的元组之一



编辑:如果要保留订购,上述单行将不起作用,因为设置将不会这样做。然而,使用几行代码,您也可以这样做:

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

see = set()
new_l = []
在l中:d
t = tuple(d.items())
如果没有看到:
.add(t)
new_l.append(d)

print new_l

示例输出:

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

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


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

For this list: [{'a': 123}, {'b': 123}, {'a': 123}]

I'd like to return this: [{'a': 123}, {'b': 123}]

Another example:

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

I'd like to return this: [{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}]

解决方案

Try this:

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

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 and, after that, re-create the dictionaries from tuples with dict.

where:

  • l is the original list
  • d is one of the dictionaries in the list
  • t is one of the tuples created from a dictionary

Edit: 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

Example output:

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

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列表中删除重复的dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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