程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python deep and shallow copy

編輯:Python
import copy
name_list = ["kill", "ait", ["jack", "wang", "woh"], "hell"]
# Shallow copy 
name1 = name_list.copy()
""" The following three also belong to Shallow copy name1 = copy.copy(name_list) name1 = name_list[:] name1 = list(name_list) """
print(name1)
name_list[1] = "wangjjj"
name_list.append("worllld")
print(name_list)
print(name1)
name_list[2][1] = " Excellent "
name_list[2].append(" Su Qin ")
print(name_list)
print(name1)
print(" Split line ----------------")
# Deep copy ( Careful use , When the amount of data is large , Deep copy takes up a lot of memory )
name_list2 = ["alex", "ait", ["jack", "wang", "woh"], "hell"]
name2 = copy.deepcopy(name_list2)
print(name2)
name_list2[1] = "wangjjj"
name_list2.append("worllld")
print(name_list2)
print(name2)
name_list2[2][1] = " Excellent "
name_list2[2].append(" Su Qin ")
print(name_list2)
print(name2)
print(" Split line -------------")
# Simple application of shallow copy : Husband and wife share an account , Different names , But deposits are the same 
person = ['name', ['saving', 100]]
husband = person[:]
wife = person[:]
husband[0] = ' I am my husband '
wife[0] = ' I am a wife '
print(husband)
print(wife)
# My husband spent 30 block , be left over 70 block , My wife's account also changes with me 
husband[1][1] = 70
print(husband)
print(wife)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved