我有一段代码,其中我有一个优先队列,我正在尝试克隆它以进行遍历。
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时,此时优先队列为空。有人可以指出代码哪里出错了吗?
PriorityQueue
,但可以尝试使用copy.deepcopy()
。 - Markus Meskanen