用Python打印队列的内容

2024-05-13 21:26:15 发布

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

如果我使用的是python模块queue.queue,我希望能够使用不弹出原始队列或创建新队列对象的方法打印出内容。

我试过做一个获取,然后把内容放回去,但这是太高的成本。

# Ideally it would look like the following
from queue import Queue
q = Queue()
q.print()
q.put(1)
q.print()

>> [] # Or something like this
>> [1] # Or something like this

Tags: 模块or对象方法内容队列queueit
2条回答

假设你用的是puthon2。 你可以用这样的东西:

from queue import Queue
q = Queue.Queue()
q.put(1)
q.put(2)
q.put(3)
print q.queue

您也可以在上面循环:

for q_item in q.queue:
    print q_item

但是,除非您正在处理线程,否则我将使用普通列表作为队列实现。

>>> print(list(q.queue))

这对你有用吗?

相关问题 更多 >