Python3 队列 发表于 2019-04-03 | collections.dequedeque提供了popleft、appendleft等函数 12345678910111213141516171819202122232425262728293031from collections import dequeq = deque([1, 2, 3])len(q) # 3q.append(1) # deque([1, 2, 3, 1])q.appendleft(4) # deque([4, 1, 2, 3, 1])right = q.pop() # right == 1, q == deque([4, 1, 2, 3])left = q.popleft() # left == 4, q == deque([1, 2, 3])q.extend([9, 8]) # q == deque([1, 2, 3, 9, 8])q.extendleft([1, 2]) # q == deque([2, 1, 1, 2, 3, 9, 8]) 这里注意被extend的list是倒序插入的q.count(1) # 2q.index(3) # 4q.insert(1, 5) # q == deque([2, 5, 1, 1, 2, 3, 9, 8]) 指定索引插入元素q.remove(1) # q == deque([2, 5, 1, 2, 3, 9, 8]) 移除第一个匹配的元素q.reverse() # q == deque([8, 9, 3, 2, 1, 5, 2])q.rotate() # q == deque([2, 8, 9, 3, 2, 1, 5])q.rotate(3) # q == deque([2, 1, 5, 2, 8, 9, 3]) 将结尾三个元素旋转至开始