[英]Remove specific duplicates from list with Python
Let's say I have a list of objects in which I want to remove duplicates that have the same id
and then keep the one that have public: true
假设我有一个对象列表,我想在其中删除具有相同
id
重复项,然后保留具有public: true
recipes = [
{'id': 1, 'public': True},
{'id': 1, 'public': False},
{'id': 2, 'public': False},
{'id': 3, 'public': True},
{'id': 3, 'public': False}
]
list_ = []
[list_.append(r) for r in recipes if r.id not in list_]
print(list_)
What would be the second condition to add?要添加的第二个条件是什么?
Wanted result:想要的结果:
recipes = [
{'id': 1, public: True},
{'id': 2, public: False},
{'id': 3, public: True},
]
You can use count
to check if the entry is a duplicate.您可以使用
count
来检查条目是否重复。
recipes = [
{'id': 1, 'public': True},
{'id': 1, 'public': False},
{'id': 2, 'public': False},
{'id': 3, 'public': True},
{'id': 3, 'public': False}
]
list = []
[list.append(r) for r in recipes if [i['id'] for i in recipes].count (r['id']) == 1 or r['public']]
return list
This answer has O(n) time, hope it's enough:这个答案有 O(n) 时间,希望足够了:
ids={} # Use a set to check for dups
l=[] # Answer list
for i in recipes: # O(n) time
if i['id'] in ids:
if i['public']:
ids[i['id']]=i
else:
ids[i['id']]=len(l) # renumber the index for later O(1) use
# len(l) is a O(1) function, so no need to keep a counter
l.append(i)
Sorry it's not a one-liner,but I think this is good practice :)对不起,这不是单线,但我认为这是一个很好的做法:)
You could first sort the list according to the 'public'
key, in reverse order, so that all the 'public': True
entries are on top.您可以首先根据
'public'
键以相反的顺序对列表进行排序,以便所有'public': True
条目都在顶部。 Then do a list comprehension to filter out unique 'id'
values sequentially:然后进行列表理解以按顺序过滤掉唯一的
'id'
值:
recipes = [{'id': 1, 'public': True},
{'id': 1, 'public': False},
{'id': 2, 'public': False},
{'id': 3, 'public': True},
{'id': 3, 'public': False}]
recipes.sort(key = lambda r: r['public'], reverse=True)
recipes_unique = []
[recipes_unique.append(r) for r in recipes if
r['id'] not in [s['id'] for s in recipes_unique]]
display(recipes_unique)
This delivers the desired result (if necessary, you could of course sort by 'id'
again):这将提供所需的结果(如有必要,您当然可以再次按
'id'
排序):
[{'id': 1, 'public': True},
{'id': 3, 'public': True},
{'id': 2, 'public': False}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.