class Node:
key = None
value = None
pre = None
next = None def __init__(self, key, value):
self.key = key
self.value = value class LRUCache:
capacity = 0
mapping = {}
head = None
end = None def __init__(self, capacity):
self.capacity = capacity def set(self, key, value):
if key in self.mapping:
oldNode = self.mapping[key]
oldNode.value = value
self.remove(oldNode)
self.set_head(oldNode)
else:
node = Node(key, value)
if len(self.mapping) >= self.capacity:
self.mapping.pop(self.end.key)
self.remove(self.end)
self.set_head(node)
else:
self.set_head(node)
self.mapping[key] = node def set_head(self, n):
n.next = self.head
n.pre = None
if self.head:
self.head.pre = n
self.head = n
if not self.end:
self.end = self.head def remove(self, n):
if n.pre:
n.pre.next = n.next
else:
self.head = n.next if n.next:
n.next.pre = n.pre
else:
self.end = n.pre def get_all_keys(self):
tmp_node = None
if self.head:
tmp_node = self.head
temp = "<{0},{1}>"
result = ""
while tmp_node:
tmp = temp.format(tmp_node.key, tmp_node.value)
result += tmp
result += " -> "
tmp_node = tmp_node.next
tmp = temp.format(None, None)
result += tmp
print(result) def get(self, key):
if key in self.mapping:
node = self.mapping[key]
self.remove(node)
self.set_head(node)
return node.value
else:
return -1 if __name__ == '__main__':
cache = LRUCache(100) cache.set('a', '1')
cache.set('b', '2')
cache.set('c', 3)
cache.set('d', 4) cache.get_all_keys() cache.set('a', 5)
cache.get_all_keys() cache.get('c')
cache.get_all_keys()

Python实现一个LRU的更多相关文章

  1. 用Python写一个简单的Web框架

    一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...

  2. python 获取一个列表有多少连续列表

    python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:

  3. python是一个解释器

    python是一个解释器 利用pip安装python插件的时候,观察到python的运作方式是逐步解释执行的 适合作为高级调度语言: 异常的处理以及效率应该是主要的问题

  4. 使用python检测一个设备是否ping的通

    使用python检测一个设备是否ping的通 一,subprocess以及常用的封装函数 运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并 ...

  5. python 登陆一个网站

    今天想用python写一个登陆的脚本,搜了一下,网上挺多的,看了一些后写了个登陆虎扑论坛的脚本. 原理: 只要在发送http请求时,带上含有正常登陆的cookie就可以了. 1.首先我们要先了解coo ...

  6. Python开发一个csv比较功能相关知识点汇总及demo

    Python 2.7 csv.reader(csvfile, dialect='excel', **fmtparams)的一个坑:csvfile被csv.reader生成的iterator,在遍历每二 ...

  7. Pyscripter是python下一个非常流行的开源IDE

    Pyscripter 不能正确调用另一文件中模块的问题的解析(Internal Engine 和 Remote Engine) 背景 Pyscripter是python下一个非常流行的开源IDE,笔者 ...

  8. python socket编程---从使用Python开发一个Socket示例说到开发者的思维和习惯问题

    今天主要说的是一个开发者的思维和习惯问题. 思维包括编程的思维和解决一个具体问题的分析思维,分析思路,分析方法,甚至是分析工具. 无论是好习惯还是不好的习惯,都是在者一天一天的思维中形成的.那些不好的 ...

  9. python遍历一个目录,输出所有文件名

    python遍历一个目录,输出所有文件名 python os模块 os import os  def GetFileList(dir, fileList):  newDir = dir  if os. ...

随机推荐

  1. Windows10_64位搭建WampServer(运行php代码)教程及问题

    Windows10_64位搭建WampServer(运行php代码)教程及问题    笔者最近学习PHP,想通过web页面的形式更加形象生动的了解php代码的原理.     于是,这次就通过WampS ...

  2. 线段树 区间查询最大值,单体修改 hdu 1754

    #include<cstdio> #include<algorithm> #include<string.h> #include<math.h> #in ...

  3. VS2015生成代码图

    熟悉代码调用流程,可以在调试结束前显示代码图,操作位置: 比如开源的caffe_ocr的代码图:

  4. ALSA driver--PCM实例创建框架

    在介绍PCM 之前,我们先给出创建PCM实例的框架. #include <sound/pcm.h> .... /* hardware definition */ static struct ...

  5. laravel Excel导入导出

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和导出. 该 ...

  6. eclipse的版本代号

    mars为4.5版本号 代号 代号名 发布日期Eclipse 3.1 IO 木卫一,伊奥 2005Eclipse 3.2 Callisto 木卫四,卡里斯托 2006Eclipse 3.3 Europ ...

  7. JEECG用户录入时用户账号长度修改

    JEECG用户账号默认长度为10字符,但实际运用中很大可能大于10字符. 解决方法: 1.找到\webpage\system\user\user.jsp文件 <input id="us ...

  8. JDK-13下载安装及环境变量配置

    1.JDK-13下载安装及环境变量配置 直接去官网下载 附下载链接:https://www.oracle.com/technetwork/java/javase/downloads/index.htm ...

  9. MSComm控件进行串口编程的基本步骤

    Visual C++为我们提供了一种好用的ActiveX控件Microsoft Communications Control(即MSComm)来支持应用程序对串口的访问,在应用程序中插入MSComm控 ...

  10. reduce 方法(升序)

    语法: array1.reduce(callbackfn[, initialValue]) 参数 定义 array1 必需.一个数组对象. callbackfn 必需.一个接受最多四个参数的函数.对于 ...