python-depth Copy

Source: Internet
Author: User

Stored procedures that need to understand the data:

Data is stored on disk in two areas, one is a block of data, the data is stored, and one is an index tag. The data is read by the index tag to find the location of the data block, thus finding the data.

1, first look at the assignment operation.

L1 = [1,2,3,['Barry','Alex']]l2=L1l1[0]= 111Print(L1)#[111, 2, 3, [' Barry ', ' Alex ']Print(L2)#[111, 2, 3, [' Barry ', ' Alex ']l1[3][0] ='Wusir'Print(L1)#[111, 2, 3, [' Wusir ', ' Alex ']Print(L2)#[111, 2, 3, [' Wusir ', ' Alex ']

For an assignment operation, L1 and L2 point to the same memory address, so they are exactly the same.

2, shallow copy copy.

L1 = [1,2,3,['Barry','Alex']]l2=l1.copy ()Print(L1,id (L1))#[1, 2, 3, [' Barry ', ' Alex ']] 2380296895816Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2380296895048L1[1] = 222Print(L1,id (L1))#[1, 222, 3, [' Barry ', ' Alex ']] 2593038941128Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2593038941896l1[3][0] ='Wusir'Print(L1,id (l1[3]))#[1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016Print(L2,id (l2[3]))#[1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016

For shallow copy, the first layer creates a new memory address, and from the second level it points to the same memory address, so consistency is maintained for the second layer and the deeper layers.

3, deep copy deepcopy.

ImportCOPYL1= [1,2,3,['Barry','Alex']]l2=copy.deepcopy (L1)Print(L1,id (L1))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167816Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[1] = 222Print(L1,id (L1))#[1, 222, 3, [' Barry ', ' Alex ']] 2915377167816Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[3][0] ='Wusir'Print(L1,id (l1[3]))#[1, 222, 3, [' Wusir ', ' Alex ']] 2915377167240Print(L2,id (l2[3]))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167304

For deep copy, the two are completely independent, changing any element of any one (no matter how many layers), and the other absolutely does not change

4. Other operations

For loop: The user iterates through the contents of an object in order.

msg ='old boy Python is the best Python training organization in the country' forIteminchmsg:Print(item) Li= ['Alex','Silver Corner','Goddess','Egon','Taibai'] forIinchLi:Print(i) DIC= {'name':'Taibai',' Age': 18,'Sex':'Mans'} forKvinchDic.items ():Print(K,V)

Enumerate: enumerations, for an iterative (iterable)/traversed object (such as a list, string), enumerate it into an index sequence that can be used to obtain both the index and the value.

Li = ['Alex','Silver Corner','Goddess','Egon','Taibai'] forIinchEnumerate (LI):Print(i) forIndex,nameinchEnumerate (li,1):    Print(Index,name) forIndex, nameinchEnumerate (li, 100):#The starting position is 0 by default and can be changed    Print(Index, name)

Range: Specifies a range that generates the specified number.

 for  in range (1,10):    print(i) for in range (1,10,2):  #  step    print(i)for in#  Reverse Step    Print(i)

Reference Link: http://www.cnblogs.com/jin-xin/articles/7562422.html

python-depth 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.