问题描述
对于此列表: [{'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])]
策略是将字典列表转换为元组列表,其中元组包含字典的项目。由于可以使用元组进行散列,因此您可以使用 其中: 编辑:如果要保留订购,上述单行将不起作用,因为 示例输出: 注意:正如@alexis所指出的那样,可能会发生具有相同键和值的两个字典不会导致相同的元组。如果他们通过不同的添加/删除密钥历史记录可能会发生这种情况。如果是这样的问题,请考虑按照他的建议排序 I have a list of dicts, and I'd like to remove the dicts with identical key and value pairs. For this list: I'd like to return this: Another example: For this list: I'd like to return this: Try this: 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 where: Edit: If you want to preserve ordering, the one-liner above won't work since Example output: 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 这篇关于在Python列表中删除重复的dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! set
删除重复项,之后,使用 dict $ c重新创建字典$ c $。
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}]
d.items()
。[{'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 set([tuple(d.items()) for d in l])]
set
and, after that, re-create the dictionaries from tuples with dict
.l
is the original listd
is one of the dictionaries in the listt
is one of the tuples created from a dictionaryset
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}]
d.items()
as he suggests.