# add a new element to orignal 1D list aimport copy a = [1, 2]b = a c = a.copy() d = a[:]e = list(a)f = copy.copy(a)g = copy.deepcopy(a) print("add a new element to orignal 1D list a")a.append(3)print("original a = ", a)print("assign b = ", b)print("built-in c = ", c) print("slice d = ", d) print("list() e = ", e) print("copy.copy() f = ", f) print("copy.deepcopy() g = ", g) # add a new element to orignal 1D list a# original a = [1, 2, 3]# assign b = [1, 2, 3]# built-in c = [1, 2]# slice d = [1, 2]# list() e = [1, 2]# copy.copy() f = [1, 2]# copy.deepcopy() g = [1, 2]# change first element in original 1D list aimport copyprint("change first element in original 1D list a")a = [1, 2]b = a c = a.copy() d = a[:]e = list(a)f = copy.copy(a)g = copy.deepcopy(a)a[0] = 5print("original a = ", a)print("assign b = ", b)print("built-in c = ", c) print("slice d = ", d) print("list() e = ", e) print("copy.copy() f = ", f) print("copy.deepcopy() g = ", g)# change first element in original 1D list a# original a = [5, 2]# assign b = [5, 2]# built-in c = [1, 2]# slice d = [1, 2]# list() e = [1, 2]# copy.copy() f = [1, 2]# copy.deepcopy() g = [1, 2]# add a new element to orignal 2D list aimport copy a = [[1, 2], [3, 4]]b = a # 终生绑定c = a.copy() #暂时绑定d = a[:] # 一维 deepcopy 二维 shallow copye = list(a)f = copy.copy(a)g = copy.deepcopy(a) print("add a new element to orignal 2D list a")print("idx_a:",id(a))print("idx_d:",id(d))a.append([5, 6])a[0][1] = 999print("idx_a:",id(a))print("idx_d:",id(d))print("original a = ", a)print("assign b = ", b)print("built-in c = ", c) print("slice d = ", d) print("list() e = ", e) print("copy.copy() f = ", f) print("copy.deepcopy() g = ", g)# add a new element to orignal 2D list a# original a = [[1, 2], [3, 4], [5, 6]]# assign b = [[1, 2], [3, 4], [5, 6]]# built-in c = [[1, 2], [3, 4]]# slice d = [[1, 2], [3, 4]]# list() e = [[1, 2], [3, 4]]# copy.copy() f = [[1, 2], [3, 4]]# copy.deepcopy() g = [[1, 2], [3, 4]]# add a new element to orignal 2D list a# idx_a: 140448613644544# idx_d: 140448613505216# idx_a: 140448613644544# idx_d: 140448613505216# original a = [[1, 999], [3, 4], [5, 6]]# assign b = [[1, 999], [3, 4], [5, 6]]# built-in c = [[1, 999], [3, 4]]# slice d = [[1, 999], [3, 4]]# list() e = [[1, 999], [3, 4]]# copy.copy() f = [[1, 999], [3, 4]]# copy.deepcopy() g = [[1, 2], [3, 4]]# change first element in original 2D list aprint("change first element in original 2D list a")a = [[1, 2], [3, 4]]b = a c = a.copy() d = a[:]e = list(a)f = copy.copy(a)g = copy.deepcopy(a) print("idx_a:",id(a))print("idx_d:",id(d))a[0][0] = 5print("idx_a:",id(a))print("idx_d:",id(d))print("original a = ", a)print("assign b = ", b)print("built-in c = ", c) print("slice d = ", d) print("list() e = ", e) print("copy.copy() f = ", f) print("copy.deepcopy() g = ", g)# original a = [[5, 2], [3, 4]]# assign b = [[5, 2], [3, 4]]# built-in c = [[5, 2], [3, 4]]# slice d = [[5, 2], [3, 4]]# list() e = [[5, 2], [3, 4]]# copy.copy() f = [[5, 2], [3, 4]]# copy.deepcopy() g = [[1, 2], [3, 4]]
疑问:list赋值和append操作会造成不一样的结果?