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. XSS 2

    通过第一题之后继续进行第二题 我们会发现这个体会将内容放到<textarea></textarea>中然后我们刚才那段代码就失效了 因为这个代码可以将我们输入的内容转换成超文本 ...

  2. ARM64架构下面安装mysql5.7.22

    MySQL下载地址为: https://obs.cn-north-4.myhuaweicloud.com/obs-mirror-ftp4/database/mysql-5.7.27-aarch64.t ...

  3. Python3.6打开EAIDK-610开发板(计算机通用)摄像头拍照并保存

    环境:python3.6 代码: import cv2 import os output_dir ='/home/openailab/Desktop/huahui/came/' i = cap = c ...

  4. Python的几种主动结束程序方式

    1. sys.exit() 执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法. 该方法中包含一个参数status,默认为0,表示正常退 ...

  5. paramiko-tools

    own dev # coding=utf-8 import paramiko import os import logging import json import unittest from sta ...

  6. Ubuntu18.04安装Vim-plug与YCM

    由于个人强迫症的原因,之前的ycm是通过vundle来管理的,这次想更新一下ycm发现问题太多,于是就重新装了个Ubuntu虚拟机,用vim-plug来进行管理ycm及其他插件. 首先要换一下Ubun ...

  7. php 基础 字符型转换整形

    示例: 可以得出规律:以有效数字开头的,取有效数字.以非有效数字开头的都转换为0:

  8. opencv:图像查找表 与 颜色表

    LUT 使用 颜色查找表 example LUT applyColorMap // 读入制作好的lut.png Mat color = imread("D:/images/lut.png&q ...

  9. 201771010135 杨蓉庆/张燕《面对对象程序设计(java)》第十三周学习总结

    1.实验目的与要求 (1) 掌握事件处理的基本原理,理解其用途: (2) 掌握AWT事件模型的工作机制: (3) 掌握事件处理的基本编程模型: (4) 了解GUI界面组件观感设置方法: (5) 掌握W ...

  10. SDNU_ACM_ICPC_2020_Winter_Practice_1st

    A Petya is a big fan of mathematics, esecially its part related to fractions. Recently he learned th ...