天道酬勤,学无止境

ete3

如何在没有 __hash__ 的情况下删除对象列表中的重复项(How to remove duplicates in list of objects without __hash__)

问题 我有一个自定义对象列表,我想从中删除重复项。 通常情况下,你会被定义都做到这一点__eq__和__hash__为对象,然后取set对象的列表中。 我已经定义了__eq__ ,但我想不出一种实现__hash__的好方法,以便它为相等的对象返回相同的值。 更具体地说,我有一个派生自 ete3 工具包的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),

2021-10-19 09:55:57    分类:技术分享    python   hash   bioinformatics   equality   ete3

How to remove duplicates in list of objects without __hash__

I have a list of custom objects from which I want to remove the duplicates. Normally, you would do this by defining both __eq__ and __hash__ for your objects and then taking the set of the list of objects. I have defined __eq__, but I can't figure out a good way to implement __hash__ such that it returns the same value for objects that are equal. More specifically, I have a class that is derived from the Tree class from the ete3 toolkit. I have defined two objects to be equal if their Robinson-Foulds distance is zero. from ete3 import Tree class MyTree(Tree): def __init__(self, *args, **kwargs

2021-07-13 11:06:27    分类:问答    python   hash   bioinformatics   equality   ete3