cachetools的LRU缓存问题

2024-06-26 18:08:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用cachetools库中的LRUCache,但是当我尝试追加时,我得到错误“dict”对象没有属性“append” 虽然我知道是什么错误,但我似乎想不出任何办法来绕过它,有人能帮忙吗? 这里有一个小代码。在

GivenQuestionsCache=LRUCache(maxsize=100,missing=getGivenQuestions)
now GivenQuestionsCache[1] gives
{1: [[211736, None], [211736, 'a'], [207113, 'a'], [219556, None], [207095, None], [89027, None], [89027, None]]} 

我正在努力

^{pr2}$

然后抛出错误。有没有其他方法可以达到这个目的? 我要我的储藏室变成

{1: [[211736, None], [211736, 'a'], [207113, 'a'], [219556, None], [207095, None], [89027, None], [89027, None],[10,None]]} 

Tags: 对象代码none属性错误nowdictmissing
1条回答
网友
1楼 · 发布于 2024-06-26 18:08:50

我测试了你的代码,它工作了:

from cachetools import LRUCache
GivenQuestionsCache=LRUCache(maxsize=100,missing=lambda _: dict())
GivenQuestionsCache[1] = [[211736, None], [211736, 'a'], [207113, 'a'], [219556, None], [207095, None], [89027, None], [89027, None]]
GivenQuestionsCache[1].append([10,None])
print GivenQuestionsCache[1]

退货

^{pr2}$

但是

GivenQuestionsCache[2].append([10,None])

会回来的

AttributeError: 'dict' object has no attribute 'append'

因此,您需要检查所有可能修改givenquestioncache的代码。在

相关问题 更多 >