Skip to main content
 首页 » 编程设计

python之如何在双端队列中打印项目(python)

2025年05月04日12txw1958

我有这个代码:

import collections 
 
def last3scores(): 
    return collections.deque([], 3) 
 
user_last3 = collections.defaultdict(last3scores) 
 
#after this I have some more code and then this: 
 
user_last3[name].append(score) 
 
print(str(user_last3)) 

但是当我运行程序时,我得到了这个:

defaultdict(<function last3scores at 0x0000000003806E18>, {'nick': deque([2], maxlen=3)}) 

我想得到的是:

{'nick': [2]} 

有没有办法在 Python 3.* 中实现这一点?

请您参考如下方法:

这应该可以解决问题(在 Python 3.* 中切换到 items 而不是 iteritems):

>>> {k:list(v) for k,v in user_last3.iteritems()} 
{'nick': [2]}