Python实现一个LRU
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的更多相关文章
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- python 获取一个列表有多少连续列表
python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:
- python是一个解释器
python是一个解释器 利用pip安装python插件的时候,观察到python的运作方式是逐步解释执行的 适合作为高级调度语言: 异常的处理以及效率应该是主要的问题
- 使用python检测一个设备是否ping的通
使用python检测一个设备是否ping的通 一,subprocess以及常用的封装函数 运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并 ...
- python 登陆一个网站
今天想用python写一个登陆的脚本,搜了一下,网上挺多的,看了一些后写了个登陆虎扑论坛的脚本. 原理: 只要在发送http请求时,带上含有正常登陆的cookie就可以了. 1.首先我们要先了解coo ...
- Python开发一个csv比较功能相关知识点汇总及demo
Python 2.7 csv.reader(csvfile, dialect='excel', **fmtparams)的一个坑:csvfile被csv.reader生成的iterator,在遍历每二 ...
- Pyscripter是python下一个非常流行的开源IDE
Pyscripter 不能正确调用另一文件中模块的问题的解析(Internal Engine 和 Remote Engine) 背景 Pyscripter是python下一个非常流行的开源IDE,笔者 ...
- python socket编程---从使用Python开发一个Socket示例说到开发者的思维和习惯问题
今天主要说的是一个开发者的思维和习惯问题. 思维包括编程的思维和解决一个具体问题的分析思维,分析思路,分析方法,甚至是分析工具. 无论是好习惯还是不好的习惯,都是在者一天一天的思维中形成的.那些不好的 ...
- python遍历一个目录,输出所有文件名
python遍历一个目录,输出所有文件名 python os模块 os import os def GetFileList(dir, fileList): newDir = dir if os. ...
随机推荐
- VS2015生成代码图
熟悉代码调用流程,可以在调试结束前显示代码图,操作位置: 比如开源的caffe_ocr的代码图:
- eclipse从svn导入静态文件
1.从eclipse 选择 导入 2.选择仓库和项目,选择finish 3.选择project项目导出
- 数据库程序接口——JDBC——功能第四篇——事务之Spring事务
综述 事务的实现方式有三种,JTA,Spring事务,Web Container方式.本篇讲述Spring事务. Spring事务分为两个部分核心对象,Spring事务的实现方式. Spring事务实 ...
- nginx autoindex 配置目录浏览功能
Nginx打开目录浏览功能 yum install httpd-tools -y cd /usr/local/openrestry/nginx/conf/ htpasswd -c passwd adm ...
- 转载:ADTS header
转自:http://blog.csdn.net/tx3344/article/details/7414543 1.ADTS是个啥 ADTS全称是(Audio Data Transport Stream ...
- 不会PPT配色没关系,有这些配色网站,也能让你的PPT配色美到极致
很多小伙伴在做PPT的时候,都会被PPT的配色难倒.看到各种非常好看的颜色总是想要将其用在自己的PPT中,可是却发现,颜色和颜色之间完全不搭,自己的PPT也变得丑到不像样. 别担心,今天将分享几个非常 ...
- Error: EACCES: permission denied, open '/Users/qinmengjiao/WebstormProjects/m-kbs-app/.babelrc
表示没有访问这个文件的权限 执行命令 sudo chown -R $(whoami) ~/WebstormProjects/m-kbs-app/.babelrc 就可以解决上面的问题 以下是chown ...
- 对DensePose: Dense Human Pose Estimation In The Wild的理解
研究方法 通过完全卷积学习从图像像素到密集模板网格的映射.将此任务作为一个回归问题,并利用手动注释的面部标注来训练我们的网络.使用这样的标注,在三维对象模板和输入图像之间,建立密集的对应领域,然后作为 ...
- Linux 服务器作为Nginx web服务器常见优化参数
内核参数调整cat /etc/sysctl.conf# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/ ...
- 解析C语言编程对缓冲区的理解
转载自:http://soft.chinabyte.com/database/47/12481547.shtml 下面介绍缓冲区的知识. 一.什么是缓冲区 缓冲区又称为缓存,它是内存空间的一部分.也就 ...