Python Note:双向队列 deque
collections.deque()是Python的双向队列 当新记录加入而队列已满时会自动一处最老的记录
创建
#!/usr/bin/python3
import collections
d = collections.deque([],5)
第一个参数是 args,传入一个list 第二个参数是 kwargs,是 list的最大长度,传入 int,默认创建一个无限长的 list
添加
使用append向右边添加,appendleft向左边添加
d.append(1)
d.appendleft(5)
拓展
extend 右拓展,同理 extendleft 左拓展
d.extend([3,4,5])
d.extendleft([3,4,5])
插入
d.insert(2,9)
读取并删除
对应地pop从右边读取并删除 popleft从左边读取并删除
d.pop()
d.popleft()
清空
clear 清空
d.clear()
删除元素
remove
d.remove(3)
查找索引
d.index(5)
反转
d.reverse()
把右边的元素移到左边
rotate 传入一个 int 指定移动的多少
d.rotate(3)
本文使用 CC BY-NC-SA 4.0 授权
本文链接:https://blog.akinokae.de/archives/python-deque/