- dict. , __cmp__, __eq__ __hash__. "set" .
Here is one possible implementation, although I do not make promises about the quality of the hash procedure that I have provided:
class Thing(object):
def __init__(self, file, line, rule):
self.file = file
self.line = line
self.rule = rule
def __cmp__(self, other):
result = cmp(self.file, other.file)
if result == 0:
result = cmp(self.line, other.line)
if result == 0:
result = cmp(self.rule, other.rule)
return result
def __eq__(self, other):
return cmp(self, other) == 0
def __hash__(self):
return hash(self.file) * hash(self.line) * hash(self.rule)
def __str__(self):
return ', '.join([self.file, self.line, self.rule])
things = [ Thing(u'/file.txt', u'line 666', u'A DUPLICATE RULE'),
Thing(u'/file.txt', u'line 666', u'A DUPLICATE RULE'),
Thing(u'/uniquefile.txt', u'line 999', u'A UNIQUE RULE')]
duplicate_things = set()
unique_things = set()
for t in things:
if t in unique_things:
duplicate_things.add(t)
else:
unique_things.add(t)
If you need to return to the list, just create it from the result set:
unique_things = list(unique_things)
duplicate_things = list(duplicate_things)
This is a bit more code to create your own class like this, but may give you other options in the future if your program gets complicated.
Edit
Ok, my hands are faster than my eyes today, but I think this editing solves the problem pointed out by @nosklo