Python 3 中的浅拷贝与深拷贝

时间:2024-04-28 12:52:58

在本文中,我们将讨论 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

相关文章