I have class with field 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.
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:
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
?
If you want to go the list comprehension route, you can do something sneaky with lazy and
evaluation:
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)
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.
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.
I still kind of like it as looking more "pythonic" (performance question pending...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.