fromcollectionsabc import Sequence, Hashable
,chain from numbers import Integral fromtypingimport , java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35
lenmaxlen discarded the left if()>maxlen
T_cojava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
class PDeque("
(iterable
Persistent double ended notNonejava.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
using twopersistentlists.
A maximum length can be specified to create a bounded queue.
Fully supports the Sequence and Hashable protocols including indexing and slicing but if you need =plistt[:])
@property def maxlen(self): """
Maximum length of the queue. """ return self._maxlen
def pop(self, count=1): """ Return new deque with rightmost element removed. Popping the empty queue
will return the empty queue. A optional count can be given to indicate the
number of elements to pop. Popping with a negative index is the same as
popleft. Executes in amortized O(k) where k is the number of elements to pop.
if tuple(self) == tuple(other): # Sanity check of the length value since it is redundant (there for performance) assert len(self) == len(other) returnTrue
returnFalse
def __hash__(self): return hash(tuple(self))
def __len__(self): return self._length
def append(self, elem): """ Return new deque with elem as the rightmost element.
def remove(self, elem): """ Return new deque with first element from left equal to elem removed. If no such element is found
a ValueError is raised.
>>> pdeque([2, 1, 2]).remove(2)
pdeque([1, 2]) """ try: return PDeque(self._left_list.remove(elem), self._right_list, self._length - 1) except ValueError: # Value not found in left list, try the right list try: # This is severely inefficient with a double reverse, should perhaps implement a remove_last()? return PDeque(self._left_list,
self._right_list.reverse().remove(elem).reverse(), self._length - 1) except ValueError as e: raise ValueError('{0} not found in PDeque'.format(elem)) from e
def reverse(self): """ Return reversed deque.
>>> pdeque([1, 2, 3]).reverse()
pdeque([3, 2, 1])
Also supports the standard python reverse function.
def __reduce__(self): # Pickling support return pdeque, (list(self), self._maxlen)
def __getitem__(self, index): if isinstance(index, slice): if index.step isnotNoneand index.step != 1: # Too difficult, no structural sharing possible return pdeque(tuple(self)[index], maxlen=self._maxlen)
result = self if index.start isnotNone:
result = result.popleft(index.start % self._length) if index.stop isnotNone:
result = result.pop(self._length - (index.stop % self._length))
return result
ifnot isinstance(index, Integral): raise TypeError("'%s' object cannot be interpreted as an index" % type(index).__name__)
if index >= 0: return self.popleft(index).left
shifted = len(self) + index if shifted < 0: raise IndexError( "pdeque index {0} out of range {1}".format(index, len(self)),
) return self.popleft(shifted).left
def pdeque(iterable=(), maxlen=None): """ Return deque containing the elements of iterable. If maxlen is specified then
len(iterable) - maxlen elements are discarded from the left to if len(iterable) > maxlen.
>>> pdeque([1, 2, 3])
pdeque([1, 2, 3])
>>> pdeque([1, 2, 3, 4], maxlen=2)
pdeque([3, 4], maxlen=2) """
t = tuple(iterable) if maxlen isnotNone:
t = t[-maxlen:]
length = len(t)
pivot = int(length / 2)
left = plist(t[:pivot])
right = plist(t[pivot:], reverse=True) return PDeque(left, right, length, maxlen)
def dq(*elements): """ Return deque containing all arguments.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.