class Test(object):
def __init__(self,p1,p2):
self.p1 = p1
self.p2 = p2
def __eq__(self, other):
return (other.p1 == self.p1) and (other.p2 == self.p2)
def __hash__(self):
return (self.p1 << 64) | self.p2
lst = [Test(1,2), Test(2,3), Test(1,2)]
from collections import OrderedDict
uniq = list(OrderedDict.fromkeys(lst, 0))
print [[item.p1, item.p2] for item in uniq]
If we use objects in collections hashable, we must define __hash__and __eq__.
I used it (self.p1 << 64) | self.p2as a hash with the assumption that the numbers p1and p2will not exceed 2 ^ 64 (18446744073709551616).
, . , , ( p1 p2). , . , __hash__ OrderedDict.