曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
Python

Deque in Python: collections-deque

About deque It started because I wanted to implement a “Feeling Lucky” feature. To improve performance, I planned to use a queue. By chance I saw a discussion on Stack Overflow: “Efficiency of using a Python list as a queue” Python lists have a pop method that can implement taking items from a queue, but performance is said to be mediocre. A highly upvoted answer said You won't run out of memory using the list

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

About deque

It started because I wanted to implement a “Feeling Lucky” feature. To improve performance, I planned to use a queue. By chance I saw a discussion on Stack Overflow: “Efficiency of using a Python list as a queue”

Python lists have a pop method that can implement taking items from a queue, but performance is said to be mediocre. A highly upvoted answer said

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

So I’ll just use deque. This deque is also thread-safe. I’ll set aside the specific implementation for now and look at how it’s implemented when I have time. Here’s the official intro:

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.

Usage

Copying the docs wholesale feels too wordy… Using it is similar to a lsit, but note that a list can only append and pop on the far right, while a deque can appendleft and popleft on the left, and the time complexity is still O(1). Pretty nice~

References

_EOF_

Deque in Python: collections-deque

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

相关文章

Python-缓存机制与-functools-lru_cache

缓存是一种将定量数据加以保存以备迎合后续获取需求的处理方式,旨在加快数据获取的速度。数据的生成过程可能需要经过计算,规整,远程获取等操作,如果是同一份数据需要多次使用,每次都重新生成会大大浪费时间。所以,如果将计算或者远程请求等操作获得的数据缓存下来,会加快后续的数据获取需求。 先来一个简单的例子以了解缓存机制的概念: # -*- coding: utf-8 -*- import random

2022-05-27 · Python · 约 1 分钟

我用Qt和Python开发了一个视频合并工具

想合并视频,但是却找不到比较好用的工具,很多都要收费,我想着「这破功能还得收费?」,于是决定自己搞一个,一开始用Python做了一个,效果还不错,完美完成了我的视频合并任务,不过没有图形界面,想分享给朋友一起用也没办法,于是昨天用Qt写了个图形界面套上去,中间还是遇到一些问题的,通过这篇文章记录一下。 软件的界面就是这样啦,用Qt Designer随便拖出来的,和VS拖控件也差不了多少。 用起来挺

2021-12-18 · Python · 约 1 分钟

讨论

共 0 条

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

加载评论中…