马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 BrightXiong 于 2023-3-1 21:37 编辑
>>> # 浅拷贝:利用列表的从copy()方法、切片来实现
>>> x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> y = x.copy()
>>> x[1][1] = 0
>>> x
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
>>> y
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
>>> # 将对象拷贝时,也将对象中所有引用对象一并进行拷贝
>>> # 深拷贝:利用copy模块的deepcopy()函数来实现
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> import copy
>>> b = copy.deepcopy(a)
>>> a[1][1] = 0
>>> a
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] |