【发布时间】:2015-09-09 20:24:45
【问题描述】:
我有一段代码,其中有一个优先级队列,我正在尝试克隆以遍历,
import Queue as Q
import copy
q1 = Q.PriorityQueue()
def printQueue(q):
while not q.empty():
print (q.get()),
print ''
q1.put((5,'s'))
q1.put((2,'e'))
q1.put((0,'a'))
q1.put((0,'z'))
printQueue(copy.copy(q1))
print 'second'
printQueue(copy.copy(q1))
我在网上发现可以使用 copy.copy 进行克隆。但是在我的代码中,它不起作用。当我第二次调用 prinQueue 时,到那时优先级队列为空。有人可以指出代码有什么问题吗?
【问题讨论】:
-
从未听说过
PriorityQueues,但也许可以试试copy.deepcopy()? -
copy.deepcopy 不在 PriorityQueues 上运行。看看这个问题:stackoverflow.com/questions/3377202/…
-
你看答案了吗?引用您提供的链接
The queue module in Python is used for synchronizing shared data between threads. It is not intended as a data structure and it doesn't support copying (not even shallow copy).
标签: python