当前位置:网站首页>Deep copy and shallow copy [interview question 3]
Deep copy and shallow copy [interview question 3]
2022-07-05 18:25:00 【CrazyManhhh】
The difference between deep copy and shallow copy
Shallow copy , To reallocate a piece of memory , Create a new object , But the element inside is the reference of each sub object in the original object . because l2 The address of the previous object is used , So when [2,3,4] When adding data ,l2 The data of has also changed
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)
# Output results
#[1, 2, [2, 3, 4, 6], 5]
#[1, 2, [2, 3, 4, 6]]
Deep copy , To reallocate a piece of memory , Create a new object , And the elements in the original object , In a recursive way , Copy to the new object by creating a new sub object . therefore , The new object has nothing to do with the original object .
边栏推荐
- Sophon autocv: help AI industrial production and realize visual intelligent perception
- Electron installation problems
- How can cluster deployment solve the needs of massive video access and large concurrency?
- 【PaddleClas】常用命令
- 【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
- [PM2 details]
- 快速生成ipa包
- 星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
- Notes on common management commands of openshift
- Introduction to Resampling
猜你喜欢
pytorch yolov5 训练自定义数据
Record a case of using WinDbg to analyze memory "leakage"
About Statistical Power(统计功效)
Failed to virtualize table with JMeter
Memory management chapter of Kobayashi coding
含重复元素取不重复子集[如何取子集?如何去重?]
瀚升优品app翰林优商系统开发功能介绍
Simulate the hundred prisoner problem
Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
@Extension, @spi annotation principle
随机推荐
[QNX Hypervisor 2.2用户手册]6.3.2 配置VM
寻找第k小元素 前k小元素 select_k
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
Privacy computing helps secure data circulation and sharing
英语句式参考
记录Pytorch中的eval()和no_grad()
【在优麒麟上使用Electron开发桌面应】
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
nano的CAN通信
小林coding的内存管理章节
[utiliser Electron pour développer le Bureau sur youkirin devrait]
数值计算方法 Chapter8. 常微分方程的数值解
U-Net: Convolutional Networks for Biomedical Images Segmentation
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
rust统计文件中单词出现的次数
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
MATLAB中print函数使用
模拟百囚徒问题