本文共 6017 字,大约阅读时间需要 20 分钟。
collections.
deque
( [ iterable [, maxlen ] ] ) Returns a new deque object initialized left-to-right (using ) with data from iterable. If iterable is not specified, the new deque is empty.
Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
Though objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0)
and insert(0, v)
operations which change both the size and position of the underlying data representation.
New in version 2.4.
If maxlen is not specified or is None, deques may grow to an arbitrary length. Otherwise, the deque is bounded to the specified maximum length. Once a bounded length deque is full, when new items are added, a corresponding number of items are discarded from the opposite end. Bounded length deques provide functionality similar to the tail
filter in Unix. They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest.
Changed in version 2.6: Added maxlen parameter.
Deque objects support the following methods:
append
( x ) Add x to the right side of the deque.
appendleft
( x ) Add x to the left side of the deque.
clear
( ) Remove all elements from the deque leaving it with length 0.
count
( x ) Count the number of deque elements equal to x.
New in version 2.7.
extend
( iterable ) Extend the right side of the deque by appending elements from the iterable argument.
extendleft
( iterable ) Extend the left side of the deque by appending elements from iterable. Note, the series of left appends results in reversing the order of elements in the iterable argument.
pop
( ) Remove and return an element from the right side of the deque. If no elements are present, raises an .
popleft
( ) Remove and return an element from the left side of the deque. If no elements are present, raises an .
remove
( value ) Removed the first occurrence of value. If not found, raises a .
New in version 2.5.
reverse
( ) Reverse the elements of the deque in-place and then return None
.
New in version 2.7.
rotate
( n ) Rotate the deque n steps to the right. If n is negative, rotate to the left. Rotating one step to the right is equivalent to: d.appendleft(d.pop())
.
Deque objects also provide one read-only attribute:
maxlen
Maximum size of a deque or None if unbounded.
New in version 2.7.
In addition to the above, deques support iteration, pickling, len(d)
, reversed(d)
, copy.copy(d)
, copy.deepcopy(d)
, membership testing with the operator, and subscript references such as d[-1]
. Indexed access is O(1) at both ends but slows to O(n) in the middle. For fast random access, use lists instead.
Example:
This section shows various approaches to working with deques.
Bounded length deques provide functionality similar to the tail
filter in Unix:
Another approach to using deques is to maintain a sequence of recently added elements by appending to the right and popping to the left:
The rotate()
method provides a way to implement slicing and deletion. For example, a pure Python implementation of del d[n]
relies on the rotate()
method to position elements to be popped:
To implement slicing, use a similar approach applying rotate()
to bring a target element to the left side of the deque. Remove old entries with popleft()
, add new entries with extend()
, and then reverse the rotation. With minor variations on that approach, it is easy to implement Forth style stack manipulations such as dup
, drop
, swap
, over
, pick
,rot
, and roll
.
转载地址:http://iohbi.baihongyu.com/