For the shallow copy and deep copy of lists, dictionaries, tuples, numbers and strings are not shallow copy and deep copy one says. Below I directly use the code to reflect:
1 ImportCopy2 3names = ['Cahill','Teenglan','Eric','Peggie','Aalto','Baal','Sadie',4 'Gage','Hagan','Jack','Kaley','Mabel','Lacy','Nadine','Pace','Amy']5 6 #Shallow Copy7Names2 =names.copy ()8NAMES2[1] ="modified the"9 Print(names)Ten #[' Cahill ', ' Teenglan ', ' Eric ', ' peggie ', ' Aalto ', ' Baal ', ' Sadie ', One #' Gage ', ' Hagan ', ' Jack ', ' Kaley ', ' Mabel ', ' Lacy ', ' Nadine ', ' Pace ', ' Amy '] A Print(Names2) - #[' Cahill ', ' modified ', ' Eric ', ' peggie ', ' Aalto ', ' Baal ', ' Sadie ', - #' Gage ', ' Hagan ', ' Jack ', ' Kaley ', ' Mabel ', ' Lacy ', ' Nadine ', ' Pace ', ' Amy '] the Print("") - - - Print("----------------Modify the first layer-----------------------------") +Names3 = ['Cahill','Peggie',['Ursula','Val',['Zena','Babb','Eric'],'Teenglan',],'Amy'] -Names4 =names3.copy () +NAMES3[1] ="Modify the first layer" A Print(NAMES3) at #[' Cahill ', ' Modify first layer ', [' Ursula ', ' Val ', [' Zena ', ' Babb ', ' Eric '], ' Teenglan ', ' Amy '] - Print(NAMES4) - #[' Cahill ', ' peggie ', [' Ursula ', ' Val ', [' Zena ', ' Babb ', ' Eric ', ' Teenglan '], ' Amy '] - - - Print("") in Print("-----------------Modify the second layer-------------------------------") -NAMES3[2][1] ="Modifying the second tier" to Print(NAMES3) + #[' Cahill ', ' Modify the first layer ', [' Ursula ', ' Modify the second layer ', [' Zena ', ' Babb ', ' Eric '], ' Teenglan ', ' Amy '] - Print(NAMES4) the #[' Cahill ', ' peggie ', [' Ursula ', ' Modify the second layer ', [' Zena ', ' Babb ', ' Eric '], ' Teenglan ', ' Amy '] * $ Panax Notoginseng Print("") - Print("----------------------Modify the third layer-----------------------------------") theNames3[2][2][0] ="Modify the third tier" + Print(NAMES3) A Print(NAMES4) the #shallow copy copies only the first layer, starting from the second layer to copy only its shortcuts (that is, change the original file, copy the following changes). + - $ Print("") $ Print("------------------------------deep Copy------------------------------") -Namett = ['Cahill','Peggie',['Ursula','Val',['Zena','Babb','Eric'],'Teenglan',],'Amy'] -NAMETT2 =copy.deepcopy (Namett) the - Print("")Wuyi Print("--------------------------------copy layer 1th------------------------------------") theNamett[0] ="Copy Layer 1th" - Print(Namett) Wu Print(NAMETT2) - About Print("") $ Print("--------------------------------Copy layer 2nd------------------------------------") -Namett[2][0] ="Copy Layer 2nd" - Print(Namett) - Print(NAMETT2) A + the Print("") - Print("--------------------------------Copy layer 3rd------------------------------------") $Namett[2][2][0] ="Copy Layer 3rd" the Print(Namett) the Print(NAMETT2) the #deep Replication: Full multi-layer replication
Shallow copy and deep copy of Python Foundation