普通字典善于隐射,其次追踪插入顺序。而 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的更多相关文章

  1. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  2. Python collections模块总结

    Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...

  3. python之collections模块(OrderDict,defaultdict)

    前言: import collections print([name for name in dir(collections) if not name.startswith("_" ...

  4. python collections模块

    collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...

  5. Python collections 模块用法举例

    Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...

  6. Python——collections模块、time模块、random模块、os模块、sys模块

    1. collections模块 (1)namedtuple # (1)点的坐标 from collections import namedtuple Point = namedtuple('poin ...

  7. Python——collections模块

    collections模块 collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.def ...

  8. python collections模块详解

    参考老顽童博客,他写的很详细,例子也很容易操作和理解. 1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选 ...

  9. python collections 模块 之namedtuple

    namedtuple collections.namedtuple(typename, filed_name, *, rename=False, module=None) 创建一个以 typename ...

随机推荐

  1. (转)Google Protocol Buffer 的使用和原理

    转自:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html   简介 什么是 Google Protocol Buffer? ...

  2. C#让两个长度相同的数组一一对应

    比如: string a[]={"x","y","z"} "} 用字典使其一一对应 Dictionary<string, s ...

  3. Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解

    前言 java 8 中引入的两个与日期相关的新类:Period 和 Duration.两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值.他们 ...

  4. Sqlite && EF Code FIRST 终极解决方案 2019.5.17

    Sqlite && EF Code FIRST 终极解决方案 2019.5.17 包括根据模型自动生成数据库,初始化数据,模型改变时的自动数据迁移等 2019.12.25 更新 支持E ...

  5. java中的break continue

    break语句 在任何循环语句的主体部分,均可用break控制循环的流程.break用于强行退出循环,不执行循环中剩余的语句.(break语句也在switch语句中使用) public class B ...

  6. 贪心数列构造——cf1157D

    一开始将数列设置为0 1 2 3 4 5 6... 然后从左到右遍历,每位不够就增加即可 #include<bits/stdc++.h> using namespace std; #def ...

  7. Kubernetes的包管理工具Helm的安装和使用

    1.源码安装 [root@master ~]# wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.0-linux-amd64 ...

  8. JS程序的基本语法

    JS程序的基本语法 JS是区分大小写的.如:Name和name是两个变量 JS中每一条语句,一般以英文下的分号(;)结束.这个分号不是必须的.为了向PHP兼容,最好加上分号. 运算符和变量,以及操作之 ...

  9. 安装MySql社区版(35-3)

    1,https://dev.mysql.com/ --------------------------------------------------------------------------- ...

  10. Java开发系列-Cookie与Session会话技术

    概述 会话技术:当用户打开浏览器的时候,访问不同的资源,直到用户将浏览器关闭,可以认为这是一次会话.会话技术产生是由于Http请求是一个无状态的协议,它不会记录上次访问的内容,用户在访过程中难免产生一 ...