Day2-python Basic 2---the difference between a shallow copy and a deep copy

Source: Internet
Author: User
Tags ming shallow copy

Shallow copy

First, let's look at the following code:

11 >>> names = ["maqing","Peilin","xiaoming","Lilei","Hanmeimei",["maqing","Wangchuan","Lilong"]]22 >>> names2 =names.copy ()33 >>>Print(names)43 |'maqing','Peilin','xiaoming','Lilei','Hanmeimei', ['maqing','Wangchuan','Lilong']]55 >>> names[2] ="Xiao Ming"67 >>>Print(names)78 ['maqing','Peilin','Xiao Ming','Lilei','Hanmeimei', ['maqing','Wangchuan','Lilong']]89 >>>Print(Names2)910 ['maqing','Peilin','xiaoming','Lilei','Hanmeimei', ['maqing','Wangchuan','Lilong']]Ten>>> names[-1][2] ="Li Long" One>>>Print(names) A13 ['maqing','Peilin','Xiao Ming','Lilei','Hanmeimei', ['maqing','Wangchuan','Li Long']] ->>>Print(Names2) -15 ['maqing','Peilin','xiaoming','Lilei','Hanmeimei', ['maqing','Wangchuan','Li Long']]

First step: Modify the value of ' xiaoming ' in Names to Chinese "Xiao Ming", the output list found only names modified, but not modified in Names2, in line with the nature of copy.

The second step: modify the names in the level two list "Lilong" The value of "Li Long", output names and names2, we found that ' lilong ' value has been changed to Chinese, it seems that this does not conform to the nature of copy.

This copy we call a shallow copy.

There are three methods of shallow copy:

    • Using the slice [:] Action
    • Using factory functions (such as List/dir/set)
    • Using the copy () function in the Copy module
1 person = ['name', ['saving', +]]2 P1 = copy.copy (person)3 p2 = person[:]4 p3 = list (person)
Deep copy
1>>>ImportCopy2>>> names = [" would", 28, ["Python","C #","JavaScript"]]3>>> Names2 =copy.deepcopy (names)4>>> Names[1] =' -'5>>>Print(names)6[' would',' -', ['Python','C #','JavaScript']]7>>>Print(Names2)8[' would', 28, ['Python','C #','JavaScript']]9>>> Names[2][1] ="Ruby"Ten>>>Print(names) One[' would',' -', ['Python','Ruby','JavaScript']] A>>>Print(Names2) -[' would', 28, ['Python','C #','JavaScript']]

Summarize

This article describes the assignment and copying of objects, and the differences between them:

    • The assignment of an object in Python is an object reference (memory address) Pass
    • With Copy.copy (), you can make a shallow copy of the object, which duplicates the object, but still uses the original reference for the element in the object.
    • If you need to copy a container object and all of its elements (containing the element's child elements), you can use Copy.deepcopy () to make a deep copy
    • For non-container types (such as numbers, strings, and other ' atomic ' types of objects) are not copied one says
    • If the meta-ancestor variable contains only atomic type objects, it cannot be deep copied, see the following example

   

Day2-python Basic 2---the difference between a shallow copy and a deep copy

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.