返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Python

Python中的双端队列:collections-deque

关于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

曦远 (DealiAxy)约 3 分钟
目录3 个章节

关于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),很舒服~

参考资料

_EOF_

Python中的双端队列:collections-deque

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…