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. opencv:霍夫直线检测

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  2. Codeforces Round #624 (Div. 3) F

    题意: 给出n的质点,带着初位置和速度: 如果中途两点可以相遇dis(i,j)=0: 如果不可以相遇,mindis(i,j): 求n个点的两两质点最小dis(i,j)之和 思路: 因为当初位置x和速度 ...

  3. Java - Test - TestNG: Idea 添加 TestNG 依赖

    1. 概述 Idea 开发的 maven 添加 testng 依赖 2. 背景 最近复习 TestNG 尝试搭建环境 发现教材和网上很多的教程, 都是 eclipse 的 我用的是 idea 这个貌似 ...

  4. 【资源分享】Gmod-Expression2 - 自定义像素画生成

    *作者:BUI* 可自定义制作属于你的像素画(默认为Sans) 第77行的COLOR可编辑你想要的颜色(RGB值) 1,2,3,4分别代表第77行所定义的颜色(0代表不显示) 视频地址:传送链接 @n ...

  5. 68 for循环2 for循环最简单的用法

    #include <stdio.h> int main (void) { int i ; ; ; i<; i+=) //i+=2 等价于 i= i+2: { sum = sum + ...

  6. 修改eclipse工程jdk版本

    在eclipse中项目jdk版本不匹配的时候需要修改项目工程的jdk版本,但是网上的一些版本修改不是很完全,经过一些摸索之后,参考总结了我在项目中的具体配置实践 问题: 修改eclipse中的项目jd ...

  7. 用python实现文件加密功能

    生活中,有时候我们需要对一些重要的文件进行加密,Python 提供了诸如 hashlib,base64 等便于使用的加密库. 但对于日常学习而言,我们可以借助异或操作,实现一个简单的文件加密程序,从而 ...

  8. 在服务器CentOS7版本安装Nginx

    简介 经常用使用Nginx来部署我们的网站,我的服务器是CentOS7.我不喜欢使用下载一个Nginx解压包然后解压的那种,我喜欢下面的这种. 安装 yum包管理工具是不带nginx,所以得先添加,在 ...

  9. 3、高级方法(Advanced Recipes)

    学习目录:树莓派学习之路-GPIO Zero 官网地址:https://gpiozero.readthedocs.io/en/stable/recipes_advanced.html 环境:Ubunt ...

  10. Mac安装php扩展redis遇到的问题,执行phpize问题

    1.安装redis在mac OS中可以使用brew命令进行安装redis:mac OS使用brew命令安装软件安装命令:brew install redis因为我已经安装过了,这里就不在赘述.安装完之 ...