我有一个自定义对象列表,我想从中删除重复项。通常,您可以通过为对象定义__eq__
和__hash__
,然后获取对象列表的set
来执行此操作。我已经定义了__eq__
,但我无法找到一种实现__hash__
的好方法,以便它为相同的对象返回相同的值。
更具体地说,我有一个来自ete3 toolkit的Tree
类的类。如果Robinson-Foulds距离为零,我已将两个对象定义为相等。
from ete3 import Tree
class MyTree(Tree):
def __init__(self, *args, **kwargs):
super(MyTree, self).__init__(*args, **kwargs)
def __eq__(self, other):
rf = self.robinson_foulds(other, unrooted_trees=True)
return not bool(rf[0])
newicks = ['((D, C), (A, B),(E));',
'((D, B), (A, C),(E));',
'((D, A), (B, C),(E));',
'((C, D), (A, B),(E));',
'((C, B), (A, D),(E));',
'((C, A), (B, D),(E));',
'((B, D), (A, C),(E));',
'((B, C), (A, D),(E));',
'((B, A), (C, D),(E));',
'((A, D), (B, C),(E));',
'((A, C), (B, D),(E));',
'((A, B), (C, D),(E));']
trees = [MyTree(newick) for newick in newicks]
print len(trees) # 12
print len(set(trees)) # also 12, not what I want!
print len(trees)
和print len(set(trees))
都返回12,但这不是我想要的,因为有几个对象彼此相等:
from itertools import product
for t1, t2 in product(newicks, repeat=2):
if t1 != t2:
mt1 = MyTree(t1)
mt2 = MyTree(t2)
if mt1 == mt2:
print t1, '==', t2
返回:
((D, C), (A, B),(E)); == ((C, D), (A, B),(E));
((D, C), (A, B),(E)); == ((B, A), (C, D),(E));
((D, C), (A, B),(E)); == ((A, B), (C, D),(E));
((D, B), (A, C),(E)); == ((C, A), (B, D),(E));
((D, B), (A, C),(E)); == ((B, D), (A, C),(E));
((D, B), (A, C),(E)); == ((A, C), (B, D),(E));
((D, A), (B, C),(E)); == ((C, B), (A, D),(E));
((D, A), (B, C),(E)); == ((B, C), (A, D),(E));
((D, A), (B, C),(E)); == ((A, D), (B, C),(E));
((C, D), (A, B),(E)); == ((D, C), (A, B),(E));
((C, D), (A, B),(E)); == ((B, A), (C, D),(E));
((C, D), (A, B),(E)); == ((A, B), (C, D),(E));
((C, B), (A, D),(E)); == ((D, A), (B, C),(E));
((C, B), (A, D),(E)); == ((B, C), (A, D),(E));
((C, B), (A, D),(E)); == ((A, D), (B, C),(E));
((C, A), (B, D),(E)); == ((D, B), (A, C),(E));
((C, A), (B, D),(E)); == ((B, D), (A, C),(E));
((C, A), (B, D),(E)); == ((A, C), (B, D),(E));
((B, D), (A, C),(E)); == ((D, B), (A, C),(E));
((B, D), (A, C),(E)); == ((C, A), (B, D),(E));
((B, D), (A, C),(E)); == ((A, C), (B, D),(E));
((B, C), (A, D),(E)); == ((D, A), (B, C),(E));
((B, C), (A, D),(E)); == ((C, B), (A, D),(E));
((B, C), (A, D),(E)); == ((A, D), (B, C),(E));
((B, A), (C, D),(E)); == ((D, C), (A, B),(E));
((B, A), (C, D),(E)); == ((C, D), (A, B),(E));
((B, A), (C, D),(E)); == ((A, B), (C, D),(E));
((A, D), (B, C),(E)); == ((D, A), (B, C),(E));
((A, D), (B, C),(E)); == ((C, B), (A, D),(E));
((A, D), (B, C),(E)); == ((B, C), (A, D),(E));
((A, C), (B, D),(E)); == ((D, B), (A, C),(E));
((A, C), (B, D),(E)); == ((C, A), (B, D),(E));
((A, C), (B, D),(E)); == ((B, D), (A, C),(E));
((A, B), (C, D),(E)); == ((D, C), (A, B),(E));
((A, B), (C, D),(E)); == ((C, D), (A, B),(E));
((A, B), (C, D),(E)); == ((B, A), (C, D),(E));
所以我的问题是:
__hash__
有效,set(trees)
对我的案例有什么好的__hash__
实施?