链接中问题的简短版本-> https://codereview.stackexchange.com/q/227081/207512
这是该问题的简短代码片段
在 python 中使用 Branch and Bound为 0/1 背包编码时,我使用队列模块中的队列来存储所有节点....
检查队列不为空的代码片段,然后根据代码获取值并将其推送到队列中。
在这里,q = queue.Queue()
用一个空节点u(-1,0,0, 0)初始化。u2 是一个空节点,开始在每次循环迭代中存储节点。
while not q.empty():
u = q.get()
............some code ..........
q.put(u2)
display_queue(q)
..........some code..............
display_queue
定义为:
def display_queue(que):
print("\nThe Nodes in queue currently: ")
for item in list(que.queue):
print("level:{}, profit:{}, weight:{}, bound:{}".format(item.level, item.profit, item.weight, item.bound))
print()
问题:
如果 q 最初包含 [(0,0,35)]。添加(1,3,70)后,队列变为[(1,3,70),(1,3,70)]。注意:这些是随机值。请不要与输出中的数据混淆。
输出样本:假设由于某种原因队列正在增加,经过适当的计算。
我已经阅读了堆栈溢出中相关问题的答案,例如:为什么将新值添加到 list<> 会覆盖 list<> 中的先前值但无法实施建议的更正。
当我尝试实现代码的微型版本时,一切都正确。也就是说,相同的东西(几乎)但不同的输出!