Custom items for list alignment with SequenceMatcher

231 Views Asked by At

I am using SequenceMatcher for aligning two lists. Each lists' item is either tuple or integer. The requirement is, for a tuple that contains a particular integer is considered as equal. For example:

(1, 2, 3) == 1 #True
(1, 2, 3) == 2 #True

To do that, I decided to override the tuple's equality method:

class CustomTuple(tuple):
    def __eq__(self, other):
        default_eval = super(CustomTuple, self).__eq__(other)
        if default_eval is not True:
            return self.__contains__(other)
        return default_eval

Here's my sample data:

x = [22, 16, 11, 16, CustomTuple((11, 19, 20)), 16]
y = [22, 16, CustomTuple((11, 19, 20)), 16, CustomTuple((11, 19, 20)), 16]

Using SequenceMatcher, I was expecting the ratio will be 1.0 (equal). But here's the result:

>>> sm = SequenceMatcher(None, x, y)
>>> sm.ratio()
0.666666666667

But when I try to compare the list using '==' operator, the result is equal:

>>> x == y
True

Can anyone point out what's going wrong? Thanks.

0

There are 0 best solutions below