丢弃旧项目的字典 [英] dictionary that discards old items

查看:59
本文介绍了丢弃旧项目的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,


我需要一个行为类似字典的集合类,但当它b / b
到达''n''项目时它会丢弃最旧的项目的长度永远不会超过''n''。 (它用于缓存搜索结果)


我对模块执行此操作的模糊记忆,但我不记得

我在哪里读到它。任何人都可以开导我吗?

问候,


McGugan将


-
http://www.willmcgugan.com

"" .join ({''''''''''',''^'':''。''}。get(c,0)或chr(97+(ord(c)-84)%26)for c in

" jvyy * jvyyzpthtna ^ pbz")

Hi folks,

I need a collection class that behaves like a dictionary but when it
reaches ''n'' items it discards the oldest item so that the length never
goes above ''n''. (Its for caching search results)

I have a vague memory of a module that does this, but I cant remember
where I read about it. Can anyone enlighten me?
Regards,

Will McGugan

--
http://www.willmcgugan.com
"".join({''*'':''@'',''^'':''.''}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")

推荐答案

Will McGugan写道:
Will McGugan wrote:
我需要一个行为类似于字典的集合类,但是当它到达''n'项时它会丢弃最旧的项目,以便长度永远不会超过''n' 。 (它用于缓存搜索结果)

我对模块执行此操作的模糊记忆,但我无法记住
我在哪里阅读它。任何人都可以启发我吗?
I need a collection class that behaves like a dictionary but when it
reaches ''n'' items it discards the oldest item so that the length never
goes above ''n''. (Its for caching search results)

I have a vague memory of a module that does this, but I cant remember
where I read about it. Can anyone enlighten me?




你想要一个最近最少使用,或LRU,缓存。这是一个:

http://aspn.activestate.com/ASPN/Coo.../Recipe/252524

Google for< LRU python>寻找其他人。

-

Michael Hoffman



You want a Least Recently Used, or LRU, cache. Here''s one:

http://aspn.activestate.com/ASPN/Coo.../Recipe/252524

Google for <LRU python> to find others.
--
Michael Hoffman


[Will McGugan]
[Will McGugan]
我需要一个行为类似字典的集合类,但当它到达''n'项时,它会丢弃最旧的项目,以便长度永远不会超过''n''。 (用于缓存搜索结果)
I need a collection class that behaves like a dictionary but when it
reaches ''n'' items it discards the oldest item so that the length never
goes above ''n''. (Its for caching search results)



导入集合


类Cache(字典):

def __init __( self,n,* args,** kwds):

self.n = n

self.queue = collections.deque()

dict .__ init __(self,* args,** kwds)

def __setitem __(self,k,v):

self.queue.append(k)

dict .__ setitem __(self,k,v)

if len(self)> self.n:

oldk = self.queue.popleft()

del self [oldk]


#。 。 。

#对setdefault,__ delitem__,fromkeys进行类似的修改

#和其他需要的变异方法


#例子

c =缓存(3)

为'w'快速棕色狐狸跳过懒狗''。split():

c [w ] = w [:1] .upper()

print repr(c)


import collections

class Cache(dict):
def __init__(self, n, *args, **kwds):
self.n = n
self.queue = collections.deque()
dict.__init__(self, *args, **kwds)
def __setitem__(self, k, v):
self.queue.append(k)
dict.__setitem__(self, k, v)
if len(self) > self.n:
oldk = self.queue.popleft()
del self[oldk]

# . . .
# make similar modifications to setdefault, __delitem__, fromkeys
# and other mutating methods as needed

# Example
c = Cache(3)
for w in ''the quick brown fox jumped over the lazy dog''.split():
c[w] = w[:1].upper()
print repr(c)


不清楚你是否想要一个FIFO ,或读取或写入LRU,所以对于

非词典组件,你也可以((搜索食谱)

|| google)这样的术语环形缓冲区,固定大小缓存,嗯,

有界队列,循环缓冲区,deque,可能有一堆

其他人。

not clear if you want a FIFO, or a LRU on read or write, so for the
non-dictionary component of this, you could also ( (search Cookbook)
|| google) for terms like "ring buffer", "fixed-size cache", um,
"bounded queue", "circular buffer", "deque", there''s probably a bunch
of others.


这篇关于丢弃旧项目的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆