Let's say I have a list of dicts. I define "duplicates" as any two dicts in the list that have the same value for the field "id" (even if the other fields are different). How do I remove these duplicates.
假设我有一个dicts列表。我将“重复”定义为列表中具有相同值的字段“id”的任何两个dicts(即使其他字段不同)。如何删除这些重复项。
An example list would be something like:
示例列表将是这样的:
[{'name': 'John' , 'id':1}, {'name': 'Mike' , 'id':5},{'name': 'Dan' , 'id':5}]
In this case, 'Mike' and 'Dan' would be duplicates, and one of them needs to be removed. It doesn't matter which one.
在这种情况下,'Mike'和'Dan'将是重复的,其中一个需要删除。哪一个没关系。
2 个解决方案
#1
11
Dump them into another dictionary, then pull them out after.
将它们转储到另一个字典中,然后将它们拉出来。
dict((x['id'], x) for x in L).values()
#2
2
The following function's list comprehension should solve your problem.
以下函数的列表理解应该可以解决您的问题。
def f(seq):
s = set()
return [x for x in seq if x['id'] not in s and not s.add(x['id'])]
#1
11
Dump them into another dictionary, then pull them out after.
将它们转储到另一个字典中,然后将它们拉出来。
dict((x['id'], x) for x in L).values()
#2
2
The following function's list comprehension should solve your problem.
以下函数的列表理解应该可以解决您的问题。
def f(seq):
s = set()
return [x for x in seq if x['id'] not in s and not s.add(x['id'])]