Python 海关;包括「;列表方法
我想更好地处理双链接结构。此场景中的目标是将一系列值附加到列表的末尾。然后我想测试一个我知道应该在列表中的值,它应该返回true,反之亦然,我知道不应该在列表中的值。这就是我到目前为止得到的Python 海关;包括「;列表方法,python,doubly-linked-list,Python,Doubly Linked List,我想更好地处理双链接结构。此场景中的目标是将一系列值附加到列表的末尾。然后我想测试一个我知道应该在列表中的值,它应该返回true,反之亦然,我知道不应该在列表中的值。这就是我到目前为止得到的 class LinkedList: class Node: def __init__(self, val, prior=None, next=None): self.val = val self.prior = prior
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):
return len(self) == len(other) and all(
val1 == val2 for val1, val2 in zip(self, other))
def __contains__(self, value):
"""Implements `val in self`. Returns true if value is found in this list."""
return all(val1 == value for val1 in zip(self))
当我测试这段代码时,我得到了一个断言错误,它说“False is not true”,我不确定为什么我的代码能够识别出值100不在列表中,而它也说50不在列表中,并在应该返回true时返回False。您的实现是错误的。只有当链接列表中的所有值都等于给定的测试值时,
\uuuuuu contains\uuuu
函数才会返回true。也就是说,只有当链接列表中的所有值彼此相等时,才是真的:
此外,该生成器表达式中的zip()
调用是完全冗余的,这里没有将多个iterable中的元素配对
请使用以下选项:
all()any()
在iterable中找到任何一项为True时,将返回True
:
>>> any(val1 == value for val1 in ['foo', 'bar', 'baz'])
True
>>> any(val1 == value for val1 in ['foo', 'foo', 'foo'])
True
>>> any(val1 == value for val1 in ['spam', 'bar', 'baz'])
False
您的实现是错误的。只有当链接列表中的所有值都等于给定的测试值时,\uuuuuu contains\uuuu
函数才会返回true。也就是说,只有当链接列表中的所有值彼此相等时,才是真的:
此外,该生成器表达式中的zip()
调用是完全冗余的,这里没有将多个iterable中的元素配对
请使用以下选项:
all()any()
在iterable中找到任何一项为True时,将返回True
:
>>> any(val1 == value for val1 in ['foo', 'bar', 'baz'])
True
>>> any(val1 == value for val1 in ['foo', 'foo', 'foo'])
True
>>> any(val1 == value for val1 in ['spam', 'bar', 'baz'])
False
使用任意项而不是全部。另外,你不需要拉链。用任何拉链代替全部拉链。而且,你不需要拉链。
return any(v == value for v in self)
>>> any(val1 == value for val1 in ['foo', 'bar', 'baz'])
True
>>> any(val1 == value for val1 in ['foo', 'foo', 'foo'])
True
>>> any(val1 == value for val1 in ['spam', 'bar', 'baz'])
False