deque是Python中stack和queue的通用形式,也就是既能当做栈使用,又能当做双向队列,list是单向队列.
list中有的方法,在deque中都可以使用
deque特有的方法有:
from collections import deque
L=deque()
# L.append(0) #在尾部添加一个元素
# L.popleft() #头部返回并删除
# L.extendleft(iterable) #头部添加多个元素
nums=[1,2,5,3,1]
L.extend(nums)
print(L)
L.rotate(2) #移动序列末尾两位到头部
b=deque("ABCDEFG",8) #指定deque最大长度为8
print(L)
deque是双端队列,支持向两端高效地插入数据、支持随机访问,时间复杂度是o(1);
队列list的时间复杂度是o(n).