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

How to print items in deque (python)

我有这个代码:

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.* 中有没有办法做到这一点?

也许你可以尝试以下方法:

for key, value in user_last3.iteritems():
    print key, value

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

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