Reputation: 161
i'm trying to create a class does not allow duplicate entryies
class QSet(list):
def __init__(self,*args):
super(QSet, self).__init__(args[0])
self=list(set(self))
and when try to test the class duplicated entries not removed
d=["a","b","a","z","a"]
z=QList(d)
print d
print z
any suggestion to solve this Thanks
Upvotes: 0
Views: 103
Reputation: 745
There is already a class in Python for that. It is called set
d=["a","b","a","z","a"]
z=set(d)
print d
print z
Does this work for you?
Upvotes: 0
Reputation: 165282
You know that set
is exactly the class you are building, right?
>>> s = set([1,2,3,3,3])
>>> s.add(4)
>>> s
set([1, 2, 3, 4])
>>> s.add(4)
>>> s
set([1, 2, 3, 4])
Upvotes: 0
Reputation: 375734
You could do what you want with a small change to your code:
class QSet(list):
def __init__(self, a):
super(QSet, self).__init__(list(set(a)))
I changed how you use the constructor args, because its odd to accept arbitrary arguments, but then insist there be at least one, and ignore all the rest.
Upvotes: 4
Reputation: 78610
Reassigning self
doesn't actually change the object (you're just rebinding the identifier self
to a new object). You could change the line to:
self[:] = list(set(self))
Upvotes: 3