Shallow copy and deep copy of Python Foundation

Source: Internet
Author: User
Tags shallow copy

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.