如何在deque(python)中打印项目

2024-10-01 11:26:04 发布

您现在位置: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))

但当我运行程序时,我得到的是:

^{pr2}$

我想要的是:

{'nick': [2]}

在Python3.*中有没有实现这一点的方法?在


Tags: 代码importreturndefmorehavesomethis
2条回答

也许你可以试试以下几点:

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

这应该能做到这一点(在python3中,*切换到items而不是{}):

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

相关问题 更多 >