‘‘‘创建者:徐洋创建时间:2017年6月13日22:25:23代码功能:详细分析列表的浅copy和深度copy‘‘‘#导入copy模块import copylist1 = [1,2,3,["a","b","c"]]#对表1进行浅复制list2 = copy.deepcopy(list1)#copy.copy#查询表1中各个元素的内存地址address0 = id(list1[0])address1 = id(list1[1])address2 = id(list1[2])address3 = id(list1[3])address30 = id(list1[3][0])address31 = id(list1[3][1])address32 = id(list1[3][2])#查询表2中各个元素的内存地址address_0 = id(list2[0])address_1 = id(list2[1])address_2 = id(list2[2])address_3 = id(list2[3])address_30 = id(list2[3][0])address_31 = id(list2[3][1])address_32 = id(list2[3][2])# 打印出列表1的内存地址和列表2的内存地址print("---------第一次比较---------")print("列表1:",address0,address1,address2,address3,address30,address31,address32)print("列表2:",address_0,address_1,address_2,address_3,address_30,address_31,address_32)# 修改列表1中某个值list1[1]=-1#查询表1中各个元素的内存地址address0 = id(list1[0])address1 = id(list1[1])address2 = id(list1[2])address3 = id(list1[3])address30 = id(list1[3][0])address31 = id(list1[3][1])address32 = id(list1[3][2])#查询表2中各个元素的内存地址address_0 = id(list2[0])address_1 = id(list2[1])address_2 = id(list2[2])address_3 = id(list2[3])address_30 = id(list2[3][0])address_31 = id(list2[3][1])address_32 = id(list2[3][2])# 打印此时列表1的内存地址和列表2的内存地址print("---------第二次比较---------")print("列表1:",address0,address1,address2,address3,address30,address31,address32)print("列表2:",address_0,address_1,address_2,address_3,address_30,address_31,address_32)print(list1,list2)# 修改列表1中第三个元素中的值list1[3][1] = 6#查询表1中各个元素的内存地址address0 = id(list1[0])address1 = id(list1[1])address2 = id(list1[2])address3 = id(list1[3])address30 = id(list1[3][0])address31 = id(list1[3][1])address32 = id(list1[3][2])#查询表2中各个元素的内存地址address_0 = id(list2[0])address_1 = id(list2[1])address_2 = id(list2[2])address_3 = id(list2[3])address_30 = id(list2[3][0])address_31 = id(list2[3][1])address_32 = id(list2[3][2])# 打印此时列表1的内存地址和列表2的内存地址print("---------第三次比较---------")print("列表1:",address0,address1,address2,address3,address30,address31,address32)print("列表2:",address_0,address_1,address_2,address_3,address_30,address_31,address_32)# 原因分析:看图print(list1,list2)
时间: 2024-10-07 04:14:11