deque简介
deque 是一个双端列表, 如果要经常从两端操作数据, 选择deque 就比较好, 如果要实现随机访问,还是建议使用列表list.
collections.deque官方说明文档
操作简介
append()
append(x)
Add x to the right side of the deque.
import collections
mydeque=collections.deque(range(3),maxlen=10)
mydeque.append('r1')
# 输出
deque([0, 1, 2, 'r1'], maxlen=10)
appendleft(x)
Add x to the left side of the deque.
mydeque.appendleft('l1')
# 输出
deque(['l1', 0, 1, 2, 'r1'], maxlen=10)
clear()
clear()
Remove all elements from the deque leaving it with length 0.
mydeque.clear()
# 输出
deque([], maxlen=10)
copy()
copy()
Create a shallow copy of the deque.
mydeque=collections.deque(range(10),maxlen=10)
mydeque1=mydeque.copy()
# 输出
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
count(x)
count(x)
Count the number of deque elements equal to x.
mydeque.count(1)
# 输出
1
extend()
extend(iterable)
Extend the right side of the deque by appending elements from the iterable argument.
mydeque.clear()
mydeque.extend(['r'+str(i) for i in range (1,6)])
# 输出
deque(['r1', 'r2', 'r3', 'r4', 'r5'], maxlen=10)
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.
mydeque.extendleft(['l'+str(i) for i in range (1,6)])
# 输出
deque(['l5', 'l4', 'l3', 'l2', 'l1', 'r1', 'r2', 'r3', 'r4', 'r5'], maxlen=10)
index()
index(x[, start[, stop]])
Return the position of x in the deque (at or after index start and before index stop). Returns the first match or raises ValueError if not found.
mydeque.index('r1',0,9)
# 输出
5
insert(i,x)
insert(i, x)
Insert x into the deque at position i.
If the insertion would cause a bounded deque to grow beyond maxlen, an IndexError is raised.
mydeque.clear()
mydeque=mydeque=collections.deque(range(4),maxlen=10)
mydeque.insert(2,'c')
# 输出
deque([0, 1, 'c', 2, 3], maxlen=10)
pop()
pop()
Remove and return an element from the right side of the deque. If no elements are present, raises an IndexError.
mydeque.pop()
# 输出
deque([0, 1, 'c', 2], maxlen=10)
popleft()
Remove and return an element from the left side of the deque. If no elements are present, raises an IndexError.
mydeque.popleft()
# 输出
deque([1, 'c', 2], maxlen=10)
remove()
remove(value)
Remove the first occurrence of value. If not found, raises a ValueError.
mydeque.remove('c')
# 输出
deque([1, 2], maxlen=10)
reverse()
reverse()
Reverse the elements of the deque in-place and then return None.
mydeque.reverse()
# 输出
deque([2, 1], maxlen=10)
rotate()
rotate(n=1)
Rotate the deque n steps to the right. If n is negative, rotate to the left.
When the deque is not empty, rotating one step to the right is equivalent to d.appendleft(d.pop()), and rotating one step to the left is equivalent to d.append(d.popleft()).
mydeque.clear()
mydeque.extend(['r'+str(i) for i in range (1,6)])
mydeque.extendleft(['l'+str(i) for i in range (1,6)])
mydeque.rotate(-3)
deque(['r3', 'r4', 'r5', 'l5', 'l4', 'l3', 'l2', 'l1', 'r1', 'r2'], maxlen=10)
# 输出
deque(['l2', 'l1', 'r1', 'r2', 'r3', 'r4', 'r5', 'l5', 'l4', 'l3'], maxlen=10)
maxlen
maxlen
Maximum size of a deque or None if unbounded.
mydeque.maxlen
# 输出
10