当前位置:网站首页>深拷贝与浅拷贝【面试题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]]
深拷贝,是指重新分配一块内存,创建一个新的对象,并且将原对象中的元素,以递归的方式,通过创建新的子对象拷贝到新对象中。因此,新对象和原对象没有任何关联。
边栏推荐
- 漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
- Cmake tutorial Step2 (add Library)
- 哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
- How to save the trained neural network model (pytorch version)
- Teamcenter 消息注册前操作或後操作
- 神经网络自我认知模型
- Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
- Kafaka technology lesson 1
- 力扣解法汇总729-我的日程安排表 I
- Cartoon: how to multiply large integers? (I) revised version
猜你喜欢

基于YOLOv3的口罩佩戴检测

论文阅读_医疗NLP模型_ EMBERT

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

VBA drives SAP GUI to realize office automation (II): judge whether elements exist

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

Matlab reference

Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!

论文阅读_中文NLP_LTP

MySQL之知识点(七)

企业数字化发展中的六个安全陋习,每一个都很危险!
随机推荐
Oracle Recovery Tools ----oracle数据库恢复利器
Which platform of outer disk gold is regular and safe, and how to distinguish it?
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
Cmake tutorial Step2 (add Library)
mongodb(快速上手)(一)
Cartoon: how to multiply large integers? (I) revised version
Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
Independent development is a way out for programmers
请问下为啥有的表写sql能查到数据,但在数据地图里查不到啊,查表结构也搜不到
Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
mybash
力扣解法汇总1200-最小绝对差
Redis基础
Webapp development - Google official tutorial
Sentinel flow guard
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account
MATLAB查阅
ITK Example
EPM related
Flask solves the problem of CORS err