python中对列表浅复制深复制另类的理解方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中对列表浅复制深复制另类的理解方式相关的知识,希望对你有一定的参考价值。

‘‘‘
创建者:徐洋
创建时间:2017年6月13日22:25:23
代码功能:详细分析列表的浅copy和深度copy
‘‘‘
#导入copy模块
import copy
list1 = [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)















































































以上是关于python中对列表浅复制深复制另类的理解方式的主要内容,如果未能解决你的问题,请参考以下文章

python学习关于列表操作相关命令以及浅复制深复制

Python学习教程:Python列表赋值,复制,深拷贝及5种浅拷贝详解

python 复制 浅copy 深copy 的区别

python基础之浅复制与深复制

python 深复制与浅复制------copy模块

Python 列表浅拷贝与深拷贝