今天无意间发现python的双向队列模块,之前只知道list,所以就拿来 学习一下。
import collections
help(collections.deque())
Help on deque object:
class deque(builtins.object)
| deque([iterable[, maxlen]]) --> deque object
|
| A list-like sequence optimized for data accesses near its endpoints.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __bool__(self, /)
| self != 0
|
| __contains__(self, key, /)
| Return key in self.
|
| __copy__(...)
| Return a shallow copy of a deque.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self integer -- return number of occurrences of value
|
| extend(...)
| Extend the right side of the deque with elements from the iterable
|
| extendleft(...)
| Extend the left side of the deque with elements from the iterable
|
| index(...)
| D.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
|
| insert(...)
| D.insert(index, object) -- insert object before index
|
| pop(...)
| Remove and return the rightmost element.
|
| popleft(...)
| Remove and return the leftmost element.
|
| remove(...)
| D.remove(value) -- remove first occurrence of value.
|
| reverse(...)
| D.reverse() -- reverse *IN PLACE*
|
| rotate(...)
| Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| maxlen
| maximum size of a deque or None if unbounded
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
# 创建双向队列
dq = collections.deque()
# 从右端添加元素
for i in range(2,5):
dq.append(i)
dq
deque([2, 3, 4])
# 从左端添加元素
dq.appendleft(-2)
dq
deque([-2, 2, 3, 4])
# 从右端扩展一个列表元素
list1 = [6,6,6]
dq.extend(list1)
dq
deque([-2, 2, 3, 4, 6, 6, 6])
# 从左端扩展一个列表元素
dq.extendleft(list1)
dq
deque([6, 6, 6, -2, 2, 3, 4, 6, 6, 6])
# 在指定位置插入元素
dq.insert(2,'b')
dq
deque([6, 6, 'b', 6, -2, 2, 3, 4, 6, 6, 6])
# 从队列右端删除元素
dq.pop()
dq
deque([6, 6, 'b', 6, -2, 2, 3, 4, 6, 6])
# 从队列左端删除元素
dq.popleft()
dq
deque([6, 'b', 6, -2, 2, 3, 4, 6, 6])
# 计算队列中指定元素的个数
dq.count(6)
4
# 复制队列
dq1 = dq.copy()
dq1
deque([6, 'b', 6, -2, 2, 3, 4, 6, 6])
dq1.clear()
dq1
deque([])
dq
deque([6, 'b', 6, -2, 2, 3, 4, 6, 6])
# 返回队列元素所在位置
dq.index("b")
1
更多学习可以参见help(collections.deque())。