How to remove duplicates in objects?

I have a set of objects:

class Test(object):
    def __init__(self):
        self.i = random.randint(1,10)


res = set()

for i in range(0,1000):
    res.add(Test())

print len(res) = 1000

How to remove duplicates from a set of objects?

Thanks for the answers, it works:

class Test(object):
    def __init__(self, i):
        self.i = i
    #   self.i = random.randint(1,10)
    #   self.j = random.randint(1,20)

    def __keys(self):
        t = ()
        for key in self.__dict__:
            t = t + (self.__dict__[key],)
        return t

    def __eq__(self, other):
        return isinstance(other, Test) and self.__keys() == other.__keys()

    def __hash__(self):
        return hash(self.__keys())

res = set()

res.add(Test(2))
...
res.add(Test(8))

result: [2,8,3,4,5,6,7]

but how to save the order? Sets the order of support. Can I use a list instead, for example?

+5
source share
4 answers

Your objects must be hashable (i.e. must have __eq__()and __hash__()) for the sets to work correctly with them:

class Test(object):
    def __init__(self):
        self.i = random.randint(1, 10)

    def __eq__(self, other):
        return self.i == other.i

    def __hash__(self):
        return self.i

hashable, , ( __hash__()), ( __eq__() __cmp__()). Hashable, , -.

Hashability , .

 

, (, delnan):

class Test(object):
    def __init__(self):
        self.i = random.randint(1, 10)
        self.k = random.randint(1, 10)
        self.j = random.randint(1, 10)

    def __eq__(self, other):
        return (self.i, self.k, self.j) == (other.i, other.k, other.j)

    def __hash__(self):
        return hash((self.i, self.k, self.j))
+9

.

:

? . , ?

list, :

  • .
  • . if foo not in res: res.append(foo). , , , , - .
  • , . , , , .., O (N) O (1).

, , set. , , list, .

, , , , list, unique_everseen itertools .

set list ( list a set , ). , .

, API, set. - OrderedSet, collections.OrderedDict.

, , , , , ; OrderedSet ActiveState.

, , , res = set() res = OrderedSet(), .

+1

, , , , , eq:

l = []
if Test(0) not in l : 
    l.append(Test(0))

2 cts...

0

, , . , , . , , :

def dedupe(lst):
    seen = set()
    results = []
    for item in lst:
        if item not in seen:
            seen.add(item)
            results.append(item)
    return results

, , . results, yield, . lst iterable, (, ).

def dedupe(iterable):
    seen = set()
    for item in iterable:
        if item not in seen:
            seen.add(item)
            yield item
0

All Articles