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 ...
随机推荐
- Swaks伪造邮件
一.搭建邮件服务器 首先需要自己搭建邮件服务器采用的是EwoMail搭建参考链接: http://doc.ewomail.com/docs/ewomail/install 二.邮件伪造发送 swaks ...
- Linux内存 mem 和 swap
摘抄并用于自查 Linux mem/swap/buffers/cached区别 free命令相对于top,提供了更简洁的查看系统内存使用情况: # free -m mem:表示物理内存统计 buff/ ...
- @Formula
@Formula 计算临时属性. 相当于可以关联查询字段,然后放在实体中当做属性使用. 任务:在User实体层,增加一个额外的属性,来获取Test表中的name字段. 1 表结构 User表 Tes ...
- wxid 转微信号
http://yinliuquan.xyz/ http://www.huwei233.cn/contact.html 更新: 测试以上都不行,大家找淘宝吧 愿世间有情人终成眷属
- [java]反转单项链表,用O(n)时间和O(1)空间
链表数据结构 public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = ...
- redis笔记_源码_简单动态字符串SDS
参照:https://zcheng.ren/sourcecodeanalysis/theannotatedredissourcesds/#sds%E5%B0%8F%E7%BB%93 这里用char b ...
- Java 多线程 - ThreadLocal
ref: https://www.cnblogs.com/chengxiao/p/6152824.html
- php+jquery 上拉加载
<script type="text/javascript"> var resflow = true,pages =2; var ps=$("#ids&quo ...
- c# sleep 例子
using System; using System.Threading; public class arr { public static void Main() { //int[] arr; // ...
- java编程规约三
七.集合处理 1.尽量使用String对象做Map的key 2.list转数组,用list的toArray(T[] array),传入的参数array是类型完全一样的数组,大小是list.size() ...