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

标签 python list python-3.x collections deque

我有这个代码:

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]}

关于python - 如何在双端队列中打印项目(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35153766/

相关文章:

python - PyYAML 自动将某些键转换为 bool 值

python - 实例化对象并使用类定义 __init__

Python Excel 突出显示单元格差异

python - Airflow + Kubernetes Executor 资源版本太旧

c++ - 正在释放的指针未分配错误?

r - 在嵌套列表中查找索引位置以进行匹配

c - 我收到错误 : petition of a member in something that it's not an structure or an enum in C

python - 我正在尝试运行 llama 索引模型,但是当我进入索引构建步骤时 - 它一次又一次失败,我该如何解决这个问题?

python - 如何通过python更准确地聚合数据框中的值?

python - python 中如何检查输入是否为整数?