关于deque

起因是我想做一个「手气不错」的功能,为了提高性能,打算用队列实现,偶然在Stack Overflow看到一个讨论「Efficiency of using a Python list as a queue

python的list有pop方法,可以实现队列的取出功能,不过据说性能一般般,高赞回答说的

You won't run out of memory using the list implementation, but performance will be poor.

那我还是用deque吧,这个deque还是线程安全的,具体的实现先不管了,有空看看它是如何实现的,附官网介绍:

Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

使用方法

感觉照搬文档好啰嗦啊…… 使用起来和lsit差不多,不过注意一下list只能在最右边append和pop,而deque可以在左边appendleft和popleft,时间复杂度还是O(1),很舒服~

参考资料