[英]How to remove duplicates with the same 'title' field from object list in Python?
I have class with field title
.我有字段
title
课程。 I have also list of objects, but with duplicates.我也有对象列表,但有重复项。
for i in self.qList:
print(i.title)
gives:给出:
War
Law
Mummy
War
War
I want to remove War
from the list, leaving only one.我想从列表中删除
War
,只留下一个。
I tried this:我试过这个:
newlist=[]
for i in self.qList:
if i.title not in self.qList.title
newlist.append(i)
but got the error:但得到了错误:
AttributeError: 'list' object has no attribute 'title'
The easiest way to do this is keep a set
of just the titles, and check against that:最简单的方法是保留一
set
标题,然后检查:
newlist = []
titles = set()
for i in self.qList:
if i.title not in titles:
newlist.append(i)
titles.add(i.title)
Your code might be easier to read with better variable names, too;使用更好的变量名称,您的代码也可能更易于阅读; what is
qList
, or i
?什么是
qList
或i
?
If you want to go the list comprehension route, you can do something sneaky with lazy and
evaluation:如果你想走列表理解路线,你可以用懒惰
and
评估做一些偷偷摸摸的事情:
titles = set()
newlist = [i for i in self.qList if i.title not in titles and titles.add(i.title) is None]
I will leave you to puzzle out exactly how this works!我会让你弄清楚这是如何工作的!
Looks like a list comprehension to me.对我来说看起来像是一个列表理解。
newlist = set(i.title for i in self.qList)
if you really wanted a list then just "cast" it back (ok it's not really casting in python but it looks like it)如果你真的想要一个列表,那么只需将它“投射”回来(好吧,它并不是真的在 python 中投射,但它看起来像)
newlist = list(set(i.title for i in self.qList))
Also, a minor added plus of using a generator expression is that there is no "leftover" i variable that you didn't really need for anything.此外,使用生成器表达式的一个小补充是没有“剩余的” i 变量,您实际上不需要任何东西。
EDIT: the above incorrect solution is left as a reminder to myself to read the question first.编辑:上面不正确的解决方案是作为提醒我自己先阅读问题的。 The "generator based solution" to give the objects would look more like:
提供对象的“基于生成器的解决方案”看起来更像是:
new_set = set(i.title for i in self.qList)
new_list = [i for i in self.qList if i.title in new_set]
I suspect that python's optimization of the generator and list comprehension could still make this faster than the loop but accessing the i.title
attribute twice is the tradeoff.我怀疑 python 对生成器和列表理解的优化仍然可以使其比循环更快,但两次访问
i.title
属性是一种权衡。
I still kind of like it as looking more "pythonic" (performance question pending...)我仍然有点喜欢它看起来更“pythonic”(性能问题待定...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.