Python实现LRU

lru.png

数组版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class LRU:
def __init__(self):
self.arr = []
self.len = 3

def append(self, num):
if len(self.arr) == self.len:
self.arr.pop(-1)
self.arr.insert(0, num)

def read(self, content):
if content in self.arr:
tmp = self.arr.pop(self.arr.index(content))
self.arr.insert(0, tmp)
else:
self.append(content)

def __repr__(self):
return str(self.arr)


if __name__ == '__main__':
# 1, 3, 0, 3, 5, 6
l = LRU()
l.append(1)
l.append(2)
l.append(3)
print(l)
l.read(2)
print(l)
l.read(4)
print(l)

链表版