当前位置:网站首页>深拷贝与浅拷贝【面试题3】
深拷贝与浅拷贝【面试题3】
2022-07-05 17:31:00 【CrazyManhhh】
深拷贝与浅拷贝的区别
浅拷贝,指的是重新分配一块内存,创建一个新的对象,但里面的元素是原对象中各个子对象的引用。因为l2使用的是之前对象的地址,所以当[2,3,4]添加数据的时候,l2的数据也发生了改变
import copy
def cal(l):
l[-1].append(6)
l.append(5)
return l
l = [1,2,[2,3,4]]
l2 = l.copy()
print(cal(l))
print(l2)
#输出结果
#[1, 2, [2, 3, 4, 6], 5]
#[1, 2, [2, 3, 4, 6]]
深拷贝,是指重新分配一块内存,创建一个新的对象,并且将原对象中的元素,以递归的方式,通过创建新的子对象拷贝到新对象中。因此,新对象和原对象没有任何关联。
边栏推荐
猜你喜欢
Leetcode daily practice: rotating arrays
Leetcode daily question: the first unique character in the string
Mask wearing detection based on yolov3
Daily exercise: a series of dates
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
To solve the problem of "double click PDF file, pop up", please install Evernote program
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
求解为啥all(())是True, 而any(())是FALSE?
网络威胁分析师应该具备的十种能力
Knowledge points of MySQL (7)
随机推荐
较文心损失一点点性能提升很多
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
How to modify MySQL fields as self growing fields
Why is February 28 in the Gregorian calendar
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
Matlab reference
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account
Teamcenter 消息注册前操作或後操作
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
Knowledge points of MySQL (7)
Delete some elements in the array
SQL Server(2)
如何保存训练好的神经网络模型(pytorch版本)
Seven Devops practices to improve application performance
漫画:寻找无序数组的第k大元素(修订版)
Redis基础
Disabling and enabling inspections pycharm
Abnormal recovery of virtual machine Oracle -- Xi Fenfei
CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
SQL Server(2)