python collections模块 之 orderdict
普通字典善于隐射,其次追踪插入顺序。而 orderdict 更善于后者。因为 orderdict 内部维护了一个双向链表,大小会是普通字典的两倍。
增加方法:
popitem(last=True)
移除并返回一个键值对,last=True 时,后进先出,反之,先进先出。
move_to_end(key, last=True)
last=True时,将键值对移至最右。反之,移至最左。key不存在时,抛错 KeyError。
应用:
记录最后一次操作的键值对:
class LastUpdatedOrderedDict(OrderedDict):
'Store items in the order the keys were last added' def __setitem__(self, key, value):
super().__setitem__(key, value)
super().move_to_end(key)
限制记录大小,当记录数超出时,删除最少查找的key:
class LRU(OrderedDict):
'Limit size, evicting the least recently looked-up key when full' def __init__(self, maxsize=128, *args, **kwds):
self.maxsize = maxsize
super().__init__(*args, **kwds) def __getitem__(self, key):
value = super().__getitem__(key)
self.move_to_end(key)
return value def __setitem__(self, key, value):
super().__setitem__(key, value)
if len(self) > self.maxsize:
oldest = next(iter(self))
del self[oldest]
python collections模块 之 orderdict的更多相关文章
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- Python collections模块总结
Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...
- python之collections模块(OrderDict,defaultdict)
前言: import collections print([name for name in dir(collections) if not name.startswith("_" ...
- python collections模块
collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...
- Python collections 模块用法举例
Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...
- Python——collections模块、time模块、random模块、os模块、sys模块
1. collections模块 (1)namedtuple # (1)点的坐标 from collections import namedtuple Point = namedtuple('poin ...
- Python——collections模块
collections模块 collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.def ...
- python collections模块详解
参考老顽童博客,他写的很详细,例子也很容易操作和理解. 1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选 ...
- python collections 模块 之namedtuple
namedtuple collections.namedtuple(typename, filed_name, *, rename=False, module=None) 创建一个以 typename ...
随机推荐
- Flutter 类似viewDidAppear 的任务处理
前言 在任务之中 ,有些实时任务比较重的需求,需要在类似 iOS viewDidAppear 里面执行数据请求任务,如:上一个页面返回pop 后执行网络请求任务.在flutter中如何实现呢? 目前 ...
- JS对象 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
返回指定的字符串首次出现的位置 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(substring, startpos) 参 ...
- Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解
前言 java 8 中引入的两个与日期相关的新类:Period 和 Duration.两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值.他们 ...
- Google Projectsheet Planning 插件的WBS
生成 WBS的序列號 在 Sldebar中的 "WBS" 按鈕: "< WBS" 取消下級目錄 "WBS >" 生成下級目錄 G ...
- 导入导出sql结构和数据
导入导出sql结构和数据
- 计数dp+概率+大数——(抽屉问题解的个数)zoj3380
难的地方在于计数dp..给定范围[1,n]的数去填m个位置,要求不能出现超过I个相同的数, 那就用dp[i][j]表示在阶段i,已经填了j个位置的可能解法,那么只要枚举i填的位置数k∈[0,min(j ...
- VC中隐藏和显示IDC_STATIC
void CImageShowAndHideDlg::OnBnClickedButton1() //隐藏 { CWnd* pWnd = GetDlgItem(IDC_STATIC); ...
- (转)I帧,P帧,B帧 .
转:http://blog.csdn.net/abcjennifer/article/details/6577934 视频压缩中,每帧代表一幅静止的图像.而在实际压缩时,会采取各种算法减少数据的容量, ...
- android studio 正式版本
注意:以下 Android Studio 下载链接全是 dl.google.com 开头的官方下载,无需tizi,建议用浏览器直接从官方原始链接下载,不要用迅雷下载.不要用迅雷下载.不要用迅雷下载,重 ...
- 16.ajax_case08
# 抓取简书博客总阅读量 # https://www.jianshu.com/u/130f76596b02 import requests import json import re from lxm ...