如何编写equals方法

时间:2016-06-14 05:30:25

标签: python linked-list

情况:我正在努力处理双重链接结构。到目前为止,我已经掌握了很好的方法。我希望能够为这个类创建两个对象,并检查它中的每个项是否相等。我没有任何语法错误,我得到的错误有点令人困惑。所以这就是我到目前为止所做的。

class LinkedList:
    class Node:
        def __init__(self, val, prior=None, next=None):
            self.val = val
            self.prior = prior
            self.next  = next

    def __init__(self):
        self.head = LinkedList.Node(None) # sentinel node (never to be removed)
        self.head.prior = self.head.next = self.head # set up "circular" topology
        self.length = 0

    def append(self, value):
        n = LinkedList.Node(value, prior=self.head.prior, next=self.head)
        n.prior.next = n.next.prior = n
        self.length += 1

    def _normalize_idx(self, idx):
        nidx = idx
        if nidx < 0:
            nidx += len(self)
            if nidx < -1:
                raise IndexError  
        return nidx

    def __getitem__(self, idx):
        """Implements `x = self[idx]`"""
        nidx = self._normalize_idx(idx)
        currNode = self.head.next
        for i in range(nidx):
            currNode = currNode.next
        if nidx >= len(self):
            raise IndexError
        return currNode.val


    def __setitem__(self, idx, value):
        """Implements `self[idx] = x`"""
        nidx = self._normalize_idx(idx)
        currNode = self.head.next
        if nidx >= len(self):
            raise IndexError
        for i in range(nidx):
            currNode = currNode.next
        currNode.val = value

    def __iter__(self):
        """Supports iteration (via `iter(self)`)"""
        cursor = self.head.next
        while cursor is not self.head:
            yield cursor.val
            cursor = cursor.next

    def __len__(self):
        """Implements `len(self)`"""
        return self.length

    def __eq__(self, other):
        currNode = self.head.next
        currNode2 = other.head.next
        for currNode, currNode2 in zip(self, other):
            if currNode.val != currNode2.val:
                return False
        return True

测试:

from unittest import TestCase
tc = TestCase()
lst = LinkedList()
lst2 = LinkedList()

tc.assertEqual(lst, lst2)

lst2.append(100)
tc.assertNotEqual(lst, lst2)

当我测试这段代码时,我得到一个Assertion错误,说“[] == [100]”我不确定为什么我的代码会认为这是相同的,当我希望它实际检查节点中的特定值时。

2 个答案:

答案 0 :(得分:2)

zip只能达到最短的列表。您想要itertools.zip_longest,并且您不想要.val(您的迭代器已经返回实际值)。试试这个:

def __eq__(self, other):
    for val1, val2 in zip_longest(self, other):
        if val1 != val2:
            return False
    return True

或者更好?

def __eq__(self, other):
    return all(val1 == val2 for val1, val2 in zip_longest(self, other))

修改

我喜欢@BrenBarn关于首先检查长度的建议。这是一个更有效的答案:

def __eq__(self, other):
    return len(self) == len(other) and all(
        val1 == val2 for val1, val2 in zip(self, other))

答案 1 :(得分:0)

zip(self.other)只能为您提供与两个列表中较短的一样多的元素。它丢弃了较长列表的额外部分。因此,对于[] == [100]zip不提供任何元素,并且您的代码返回True而不检查任何内容。

您可以在开头查看是否列表长度不同。如果他们这样做,他们就不能平等。