Python data copying

Source: Internet
Author: User

Python data copying

#-*-Config = UTF-8-*-# copy data a = [, "a", "C"]; B =; # The address space of a and B is the same. append ("B"); B. append ("f"); print (B, a); # [1, 2, 3, 4, 5, 6, 'A', 'C', 'B ', 'F'] [1, 2, 3, 4, 5, 6, 'A', 'C', 'B ', 'F'] When a is changed, B also changes B a and also changes print (id (B), id ()); #11075528 the same address space ################################## ############ import copy; # copying is the application of data in the memory # This is the shortest copy list = [1, 2, 3, 4, ["a", "B", "c"]; list_1 = copy. copy (list); print (list, list_1) print (id (list), id (list_1); #11367752 10970440 different address spaces have different lists. append ("5"); print (list, list_1); # [1, 2, 3, 4, ['A', 'B', 'C'], '5'] [1, 2, 3, 4, ['A', 'B ', 'C'] list changed, but list_1 did not change print (id (list [0]), id (list_1 [0]); #1394274096 1394274096 the address space of their respective elements is still the same list [4]. append ("d"); print (list, list_1); # [1, 2, 3, 4, ['A', 'B', 'C ', 'D'], '5'] [1, 2, 3, 4, ['A', 'B', 'C ', 'D'] changed # Deep copy list_2 = [1, 2, 3, 4, ["a", "B", "c"]; list_3 = copy. deepcopy (list_2); print (id (list_2), id (list_3); #17402824 17402248 different print addresses (id (list_2 [4]), id (list_3 [4]); #11241032 different addresses of list_2 [4]. append ("d"); print (list_2, list_3); # [1, 2, 3, 4, ['A', 'B', 'C ', 'D'] [1, 2, 3, 4, ['A', 'B', 'C'] list_3 not changed

 

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.