deque可以构造一个固定大小的队列,当超过队列之后,会把前面的数据自动移除掉。示例如下:
q = deque(maxlen =3)
q.append(1)
q.append(2)
q.append(3)
q.append(4)
print(q)
输出如下:
还可以从左往右加
from collections import deque
q = deque(maxlen =3)
q.appendleft(1)
q.appendleft(2)
q.appendleft(3)
q.appendleft(4)
print(q)
输出
移除右边第一个值,并输出:
from collections import deque
q = deque(maxlen =3)
q.appendleft(1)
q.appendleft(2)
q.appendleft(3)
q.appendleft(4)
print(q)
print(q.pop())
print(q)
输出:
移除左边第一个值,并输出:
from collections import deque
q = deque(maxlen =3)
q.appendleft(1)
q.appendleft(2)
q.appendleft(3)
q.appendleft(4)
print(q)
print(q.popleft())
print(q)
输出: