Python 3 中的浅拷贝与深拷贝

本文介绍了Python中的浅拷贝和深拷贝概念,通过示例展示了它们的区别。简单复制会使得多个列表引用同一内存区域,而浅拷贝通过切片、`copy()`等方法创建独立的地址空间,但不复制复合对象的内部列表。深度拷贝则通过`deepcopy()`函数完全复制包括复合对象在内的所有元素,确保每个对象拥有独立的地址空间。
摘要由CSDN通过智能技术生成

在本文中,我们将讨论 Python 中浅拷贝和深拷贝之间的区别。

简单复制

让我们看一个例子:

<span style="color:#000000"><span style="background-color:#fbedbb">lst_first  = [<span style="color:#800080">'</span><span style="color:#800080">Yesterday'</span>, [<span style="color:#800080">'</span><span style="color:#800080">we'</span>, <span style="color:#800080">'</span><span style="color:#800080">used'</span>], <span style="color:#800080">'</span><span style="color:#800080">the'</span>, [<span style="color:#800080">'</span><span style="color:#800080">Python'</span>, <span style="color:#800080">'</span><span style="color:#800080">2'</span>]] 
lst_second = lst_first
lst_third  = lst_first
lst_fourth = lst_first
          
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">\nOriginal list and three copies of it:"</span>)
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_first  ="</span>, lst_first) 
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_second ="</span>, lst_second) 
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_third  ="</span>, lst_third) 
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_fourth ="</span>, lst_fourth) 
                    
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">\nLet's change the first word to "</span>Today<span style="color:#800080">"</span><span style="color:#800080"> and add the word "</span>also<span style="color:#800080">"</span><span style="color:#800080"> 
       ONLY at the end of the 4th list..."</span>)
lst_fourth [<span style="color:#000080">0</span>] = <span style="color:#800080">'</span><span style="color:#800080">Today'</span> <span style="color:#008000"><em>#</em></span><span style="color:#008000"><em> replace the word 'Yesterday' with 'Today' </em></span>
lst_fourth.append(<span style="color:#800080">'</span><span style="color:#800080">too'</span>) <span style="color:#008000"><em>#</em></span><span style="color:#008000"><em> add the word 'too'</em></span>
          
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">\nLet's see the contents of the lists again:"</span>)
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_first  ="</span>, lst_first) 
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_second ="</span>, lst_second) 
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_third  ="</span>, lst_third) 
<strong><span style="color:#0000ff">print</span></strong>(<span style="color:#800080">"</span><span style="color:#800080">lst_fourth ="</span>, lst_fourth)</span></span>

让我们看看上面代码的输出:

简单复制

哇。。。。我们只更改了“”列表中的值,但所有列表中的值都发生了变化。
为什么?!lst_fourth

在脚本的第一行中,我们创建列表,然后初始化其他三个列表。
分配给其他三个列表后,所有四个列表都引用相同的内存区域。
以后对任何一个内容的修改都会立即反映在所有其他列表的内容上。lst_firstlst_first

让我们使用内置的 Python 函数来获取每个列表对象的“标识”(唯一整数):id()

<span style="color:#000000"><span style="background-color:#fbedbb"><strong><span style="color:#0000ff">print</span></str
  • 40
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值