Python full stack day19 (function supplement)

Source: Internet
Author: User
Tags shallow copy

One, depth copy

See Copy day19-1.py

S=[1, ' Zhangsan ', ' Lisi '] #s2是s的拷贝s2 =s.copy () #打印s2和s是一样的print (S2) #修改s2s2 [0]=2# print S is the unchanged print (s) #打印s2因为修改了所以有变化print (S2) [1, ' Zhangsan ', ' Lisi '] [1, ' Zhangsan ', ' Lisi '] [2, ' Zhangsan ', ' Lisi ']

If the modified element is a list, the source list also changes day19-2.py

s = [[up], ' Zhangsan ', ' Lisi ']s3=s.copy () print (S3) print (s) s3[0][1]=3# modify S3 inside list element after source list also corresponds to change print (S3) print (s) [[1, 2] , ' Zhangsan ', ' Lisi '][[1, 2], ' Zhangsan ', ' Lisi '][[1, 3], ' Zhangsan ', ' Lisi '][[1, 3], ' Zhangsan ', ' Lisi ']

Why so, because the first modification of an immutable element corresponding to the pointer has changed, the second S and S3 point to the memory address is a variable element (list) When the list changes, but the list of memory address does not change the direction of S and S3 did not change, So modifying the first element list of S3 corresponding to the first element list of S is also changed.

This is the shallow copy, the shallow copy only copies the first layer.

Python full stack day19 (function supplement)

Related Article

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.