返回文章列表文章索引阅读全文
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 许可协议。转载请注明出处!

相关文章

Python实现给图片加水印功能

前言 最近忙得连轴转,很久没更新博客了,代码倒是没啥写,积累了好些东西,接下来一有时间就来更新吧~ 本文记录使用Python实现给图片添加水印的功能实现过程 先看效果 把公众号的封面作为素材 原图是这样的 codelab 加个水印的效果 temp 实现代码 主要实现是参考GitHub上一个项目的:https://github.com/2Dou/watermarker 用到了Pillow库,使用前请

2022-09-29 · Python · 约 1 分钟

讨论

共 0 条

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

加载评论中…