python中的LRUcache行为

2024-06-26 18:05:38 发布

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

我正在使用cachetools中的LRUCache以dict的形式存储一些数据,我很难理解缺少工厂概念的行为,有人能帮忙吗?你知道吗

GivenQuestionsCache=LRUCache(maxsize=100,missing=getGivenQuestions)
def getGivenQuestions(studentId):
    cur=db.cursor()
    cur.execute(*query*)
    questions={}
    for each in cur.fetchall():
        if(int(each[1]) in questions):
            questions[int(each[1])].append([each[3],each[4]])
        else:
            questions[int(each[1])]=[[each[3],each[4]]]
    return questions

现在,当我

print(GivenQuestionsCache[studentId])

我想是这样的

[[211736, None], [211736, 'a'], [207113, 'a'], [219556, None], [207095, None], [89027, None], [89027, None]]

但它会打印出来

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

为什么它也会和值一起打印? 另外,由于这个原因,我不能像这样使用append

GivenQuestionsCache[studentId].append([int(questionId),None])

Tags: 数据innonedict形式intquestionseach
1条回答
网友
1楼 · 发布于 2024-06-26 18:05:38

显然是嵌套dict的dict引起了这个问题,我用GivenQuestionsCache[studentId][studentId].append([int(questi‌​onId),None])解决了这个问题

相关问题 更多 >