a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(5):
b = a
for i in range(2):
b.append(b[0])
b.pop(0)
print("a: ", a)
print("b: ", b)
我遇到了一个问题,在Python 3.6.4中,我将列表设置为另一个列表的内容。然后,我会将该列表的第一项(项目0)附加到结尾,然后从头开始删除它。完成此操作后,原始列表将被覆盖! 试着弄清楚上面的代码会输出什么,然后运行它(或者看下面的输出) 我期待:
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
实际结果:
a: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
b: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
b: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
a: [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
b: [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
a: [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
b: [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]