当前位置:网站首页>深拷贝与浅拷贝【面试题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之知识点(六)
- Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
- Why is February 28 in the Gregorian calendar
- Leetcode daily question: merge two ordered arrays
- Knowledge points of MySQL (6)
- Cartoon: how to multiply large integers? (next)
- Force deduction solution summary 729- my schedule I
- 2022新版PMP考试有哪些变化?
- Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
- PMP认证需具备哪些条件啊?费用多少啊?
猜你喜欢

Abnormal recovery of virtual machine Oracle -- Xi Fenfei

Zabbix

Seven Devops practices to improve application performance

How awesome is the architecture of "12306"?

神经网络自我认知模型

mongodb(快速上手)(一)

Configure pytorch environment in Anaconda - win10 system (small white packet meeting)

What are the changes in the 2022 PMP Exam?

leetcode每日一练:旋转数组

解决“双击pdf文件,弹出”请安装evernote程序
随机推荐
解决“双击pdf文件,弹出”请安装evernote程序
ITK Example
企业数字化发展中的六个安全陋习,每一个都很危险!
Abnormal recovery of virtual machine Oracle -- Xi Fenfei
漫画:如何实现大整数相乘?(下)
求解为啥all(())是True, 而any(())是FALSE?
MySQL之知识点(七)
Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
How awesome is the architecture of "12306"?
Accuracy of BigDecimal Division
为什么阳历中平年二月是28天
QT控制台打印输出
毫无章法系列
Matlab reference
Disabling and enabling inspections pycharm
Which platform of outer disk gold is regular and safe, and how to distinguish it?
证券网上开户安全吗?证券融资利率一般是多少?
得知女儿被猥亵,35岁男子将对方打至轻伤二级,法院作出不起诉决定
Read the history of it development in one breath
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!