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

相关文章

编写爬虫自动下载王者荣耀官网上好看的壁纸

前言 偶然间发现王者荣耀的官网上发布了这么多好看的壁纸。 地址:http://pvp.qq.com/web201605/wallpaper.shtml 看了一下,每一张都挺喜欢的,而且还有十多页,一张一张去慢慢下载肯定不是作为一个Geeker的正确姿势。 由于最近python用得比较多,所以看到什么好玩的就想爬虫下载下来,所以看到这么多好看图片,我又“熟练”地掏出了python。 分析 这个网站长

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

Python-缓存机制与-functools-lru_cache

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

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

关于Python的异常处理

前言 异常处理在任何一门编程语言里都是值得关注的一个话题,良好的异常处理可以让你的程序更加健壮,清晰的错误信息更能帮助你快速修复问题。在Python中,和部分高级语言一样,使用了try/except/finally语句块来处理异常,如果你有其他编程语言的经验,实践起来并不难。 Python 中(至少)有两种错误:语法错误和异常( syntax errors 和 exceptions )。下面稍微介

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

讨论

共 0 条

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

加载评论中…