python淺復制與深復制_Python中的淺復制與深復制

python淺復制與深復制

In python, the assignment operator does not copy the objects, instead, they create bindings between an object and the target. The object, if a collection that is mutable or consists of mutable items uses the copy so that one can change one copy without changing the other.

在python中,賦值運算符不復制對象,而是在對象和目標之間創建綁定。 如果該對象是可變的或由可變項組成的集合,則使用該副本,以便一個副本可以更改一個副本而無需更改另一個副本。

The module copy provides generic shallow and deep copy operations,

模塊復制提供通用的淺層復制和深層復制操作

淺拷貝 (Shallow Copy)

A shallow copy constructs a new compound object and then inserts references into it to the objects found in the original. This process is not recursive and hence doesn't create copies of the child object. In shallow copy, a reference of object is copied to another object, meaning any changes made to the copy of the object shall reflect on the original object too.

淺表副本將構造一個新的復合對象,然后將對原始對象中找到的對象的引用插入其中。 此過程不是遞歸的,因此不會創建子對象的副本。 在淺表復制中,將對象的引用復制到另一個對象,這意味著對對象副本所做的任何更改也應反映在原始對象上。

    copy.copy(x)  # returns shallow copy

Example:

例:

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import copy
>>> xs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> ys = copy.copy(xs)
>>> print(xs)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> print(ys)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> xs.append(['new sublist']) # Modifying the copied list at a "superficial" level was no problem at all.
>>> print(xs)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], ['test']]
>>> print(ys)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> xs[1][0] = 'X1' // changes both 'xs' and 'ys'
>>> print(xs)
[[1, 2, 3], ['X1', 5, 6], [7, 8, 9], ['test']]
>>> print(ys)
[[1, 2, 3], ['X1', 5, 6], [7, 8, 9]]

深拷貝 (Deep Copy)

A deep copy constructs a new compound object and then recursively inserts the copies into it the objects found in the original.

深層副本會構造一個新的復合對象,然后將原始對象中找到的對象遞歸地插入副本。

    copy.deepcopy(x) # returns a deep copy

Example:

例:

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import copy
>>> ys = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> zs = copy.deepcopy(ys)
>>> ys[1][1] = 'X'
>>> print(ys)
[[1, 2, 3], [4, 'X', 6], [7, 8, 9]]
>>> print(zs)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>

重要提醒 (Important reminders)

  • Shallow copying of an object will not clone the child objects. Hence, the child copy is not fully independent of the parent.

    淺復制對象不會克隆子對象。 因此,子副本并不完全獨立于父副本。

  • A deep copy of an object will recursively clone the child object and hence the child object is fully independent of the parent. Creating a deep copy is slower.

    對象的深層副本將遞歸克隆子對象,因此子對象完全獨立于父對象。 創建深層副本比較慢。

  • An arbitrary object, including custom classes, can be copied using the copy module.

    可以使用復制模塊復制包括定制類在內的任意對象。

翻譯自: https://www.includehelp.com/python/shallow-copy-vs-deep-copy.aspx

python淺復制與深復制

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540888.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540888.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540888.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

邏輯回歸 數據_數據科學中的邏輯回歸

邏輯回歸 數據邏輯回歸 (Logistic Regression) Logistic regression is an applied mathematics analysis methodology accustomed to predict a data price supported previous observations of a data set. Logistic regression has become a very important tool within the…

Dede?刪除文檔同時文章中的圖片的方法

首先,在"/include"目錄下建立"extend.func.php"文件. 然后,將以下內容保存在"extend.func.php"文件中,一共三個函數://解析body數據,獲得所有圖片的絕對地址function GetPicsTruePath($body,$litpic){$delfiles array();…

《linux操作系統》第06章在線測試,Linux系統管理一測試題-附答案.doc

Linux系統管理一測試題-附答案Linux系統管理一測試題姓名:班級:考試時間180分鐘,ls,ifconfig,hostname,cd的程序文件在哪里which mkdir ls ifconfig hostname cd查看當前的PATH變量的值echo $PATH在根下新建一個目錄study,在study目錄下建子目…

java8-02-Stream-API

[TOC] 0 Stream簡介 家庭住址 &#xff1a;java.util.stream.Stream<T>出生年月&#xff1a;Java8問世的時候他就來到了世上主要技能&#xff1a;那可以吹上三天三夜了…… 主要特征 不改變輸入源中間的各種操作是lazy的(惰性求值、延遲操作)只有當開始消費流的時候&…

跟隨者數字解碼_跟隨模式的數字

跟隨者數字解碼Problem statement: 問題陳述&#xff1a; Given a pattern containing only Is and Ds. I stands for increasing and D for decreasing. Devise an algorithm to print the minimum number following that pattern. Digits are from 1-9 and digits cant repe…

Linux內核機器ID,linux-如何強制內核重新讀取/重新初始化PCI設備ID?

我的機器(正在運行Linux內核3.2.38的計算機)在引導時具有錯誤的PCI設備的子系統ID(子設備和子供應商ID).如果我然后在系統仍處于啟動狀態(即熱插拔)時物理地拔出PCI設備并重新插入,則它將獲得正確的ID.請注意,錯誤的子設備ID和子供應商ID與設備的設備ID和供應商ID相同(請參見下…

Android ImageButton示例代碼

1) XML File: activity_main 1)XML文件&#xff1a;activity_main <?xml version"1.0" encoding"utf-8"?><android.support.constraint.ConstraintLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"…

IIS 偽靜態下 利用PHP獲取 網址后綴

$_SERVER[HTTP_X_ORIGINAL_URL];轉載于:https://www.cnblogs.com/paddygege/p/7238228.html

kotlin 小數位數_Kotlin程序生成4位數OTP

kotlin 小數位數OTP stands for "One Time Password" is a 4-8 digit alphanumeric code which is sent to the user via email or phone number for validation. As the name suggests, it can be used once only. OTP代表“ 一次密碼”&#xff0c;它是4-8位的字母…

NestedScrolling機制

2019獨角獸企業重金招聘Python工程師標準>>> NestedScrolling機制(可以稱為嵌套滾動或嵌套滑動)能夠讓父view和子view在滾動時進行配合&#xff0c;其基本流程如下&#xff1a; 當子view開始滾動之前&#xff0c;可以通知父view&#xff0c;讓其先于自己進行滾動;子…

linux重定向命令是干嘛的,Linux系統下重定向命令應用及其語法有什么?

1。 標準輸入的控制語法&#xff1a;命令 文件將命令的執行結果送至指定的文件中。例如&#xff1a;ls -l > list 將執行“ls -l” 命令的結果寫入文件list 中。語法&#xff1a;命令>&#xff01; 文件將命令的執行結果送至指定的文件中&#xff0c;若文件已經存在&…

kotlin 第一個程序_Kotlin程序減去兩個矩陣

kotlin 第一個程序Given two matrices, we have to subtract them. 給定兩個矩陣&#xff0c;我們必須將它們相減。 Example: 例&#xff1a; Input:matrix 1:[2, 3, 5][0, 5, 4][2, 1, 2]matrix 2:[6, 34, 2][5, 7, 5][3, 4, 3]Output:[-4, -31, 3][-5, -2, -1][-1, -3, -1]…

linux進程q是什么意思,Linux進程

#include #include #include #include #include /* 允許建立的子進程個數最大值 */#define MAX_CHILD_NUMBER 10 /* 子進程睡眠時間 */#define SLEEP_INTERVAL 2 int proc_number0; /* 子進程的自編號&#xff0c;從0開始 */void do_something();main(int argc, char* argv[]){…

cd-rom門鎖定什么意思_CD-ROM XA的完整格式是什么?

cd-rom門鎖定什么意思CD-ROM XA&#xff1a;CD-ROM擴展體系結構 (CD-ROM XA: CD-ROM Extended Architecture) CD-ROM XA is an abbreviation of "CD-ROM Extended Architecture". It is an extension, a modified version of CD-ROM, which merges compressed audio,…

linux服務chm,linux系統服務?chm

冒算發出喬家開具面霜&#xff1f;磨去開源新片米泉坎坷纜船六谷酷炫。連忙領屬官長保民涅盤肚子兇相風趣&#xff0c;逞能算圖礙事柴扉規例懲艾坡腳黃袍&#xff0c;四年幸災別稱牌號木牌&#xff0c;類乎股王藍玉求新名教年糕八股聯盟&#xff01;掛單軌跡八股落市氣功&#…

ImageView的scaleType詳解

1. 網上的誤解 不得不說很失望&#xff0c;到網上搜索了幾篇帖子&#xff0c;然后看到的都是相互復制粘貼&#xff0c;就算不是粘貼的&#xff0c;有幾篇還是只是拿著自己的幾個簡單例子&#xff0c;然后做測試&#xff0c;這種以一種現象結合自己的猜測便得出結論&#xff0c;…

IDBI的完整格式是什么?

IDBI&#xff1a;印度工業發展銀行 (IDBI: Industrial Development Bank of India) IDBI is an abbreviation of the Industrial Development Bank of India. It is an Indian financial service corporation owned and controlled by the government. In 1964, it was founded…

linux判斷內存并釋放,linux 內存清理/釋放命令

# sync# echo 1 > /proc/sys/vm/drop_cachesecho 2 > /proc/sys/vm/drop_cachesecho 3 > /proc/sys/vm/drop_cachescache釋放&#xff1a;To free pagecache:echo 1 > /proc/sys/vm/drop_cachesTo free dentries and inodes:echo 2 > /proc/sys/vm/drop_cachesT…

kei注釋_KEI的完整形式是什么?

kei注釋KEI&#xff1a;克里希納電氣工業有限公司 (KEI: Krishna Electricals Industries Limited) KEI is an abbreviation of Krishna Electricals Industries Limited. It is a public limited company that is categorized as a Non-governmental Company and the registra…

基于嵌入式linux的數碼相框的設計,基于Linux NFS的Web數碼相框設計

O 引言隨著數碼相機和互聯網的普及&#xff0c;越來越多的家庭擁有自己的媒體庫。媒體庫中既包含有自己拍攝的影像文件&#xff0c;也有從網絡上下載的影像資料。然而展示影像資料的手段單一&#xff0c;主要通過PC來實現。因此未來構建以媒體庫為中心的家庭多媒體網絡&#xf…