x = [[7, 8], 3, "hello", [6, 8], "world", 17] # List 1
w = list.copy(x) # copying list 1 to list 2
w[0][1] = 5 # changing the value in list 2
print(w)
print(x)
输出:
[[7, 5], 3, 'hello', [6, 8], 'world', 17]
[[7, 5], 3, 'hello', [6, 8], 'world', 17]
对w
的更改也会影响x
。
答案 0 :(得分:1)
from copy import deepcopy
x = [[7, 8], 3, "hello", [6, 8], "world", 17]
w = deepcopy(x)
w[0][1] = 5 # changing the value in list 2
print(w)
print(x)
结果:
[[7, 5], 3, 'hello', [6, 8], 'world', 17]
[[7, 8], 3, 'hello', [6, 8], 'world', 17]
答案 1 :(得分:0)
您需要使用copy.deepcopy()
,因为copy.copy()
仅复制列表中元素的引用。
答案 2 :(得分:0)
vect.end()
是您问题的答案:
deepcopy