如何在python中实现队列的深层复制

时间:2012-09-22 22:08:47

标签: python data-structures queue

我想维护两个队列,队列A用于弹出,队列B用于备份,所以如果我从A弹出所有对象,我该如何从B恢复队列A?

while (still has input)
     A.push(input)
     B.push(input)

while A is not empty
      A.pop()

然后如何从B中恢复A ???与此同时,我仍然希望将B作为备份。

我知道一些非常愚蠢的方法,比如再分配一个队列C然后弹出所有Bout。然后从队列C恢复B

1 个答案:

答案 0 :(得分:3)

一种方法是使用copy模块。

import copy

while (still has input)
    A.push(input)
    B.push(input)

while A is not empty
    A.pop()

A = copy.deepcopy(B)