当前位置:网站首页>深拷贝与浅拷贝【面试题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]]
深拷贝,是指重新分配一块内存,创建一个新的对象,并且将原对象中的元素,以递归的方式,通过创建新的子对象拷贝到新对象中。因此,新对象和原对象没有任何关联。
边栏推荐
- MySQL之知识点(七)
- Force deduction solution summary 729- my schedule I
- 服务器配置 jupyter环境
- Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
- Customize the theme of matrix (I) night mode
- 忽米沄析:工业互联网标识解析与企业信息系统的融合应用
- tkinter窗口预加载
- GFS分布式文件系统
- 普通程序员看代码,顶级程序员看趋势
- Neural network self cognition model
猜你喜欢

服务器配置 jupyter环境

Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions

Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?

7 pratiques devops pour améliorer la performance des applications

Knowledge points of MySQL (7)

EPM related

ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声

leetcode每日一练:旋转数组

Why is all (()) true and any (()) false?

Kafaka技术第一课
随机推荐
Accuracy of BigDecimal Division
企业数字化发展中的六个安全陋习,每一个都很危险!
Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
Troubleshooting - about clip not found Visual Studio
Career advancement Guide: recommended books for people in big factories
QT控制台打印输出
Compared with the loss of Wenxin, the performance is improved a lot
Customize the theme of matrix (I) night mode
漫画:寻找无序数组的第k大元素(修订版)
Cartoon: how to multiply large integers? (next)
ELK日志分析系统
To solve the problem of "double click PDF file, pop up", please install Evernote program
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
Teamcenter 消息注册前操作或后操作
Seven Devops practices to improve application performance
SQL Server(2)
神经网络自我认知模型
Clickhouse (03) how to install and deploy Clickhouse
Cmake tutorial step5 (add system self-test)
What are the changes in the 2022 PMP Exam?