S1 = s.copy () s1[0]= ' s1 ' s[2][0] = 2s1[2][1]=4 output: [' s ', ' sname ', [2, 4]][' s1 ', ' sname ', [2, 4]]
A shallow copy only copies the first layer, or S1 copy, to generate a new space storage s sname modification will not affect s in S1 storage ["] for address point
Shallow copy and A=b different
A=123b=ab=456print (A) print (B) output: 123456
s = [' s ', ' sname ', [1, 2]]s1 = ss1[0]= ' s1 ' s[2][0] = 2s1[2][1]=4 output: [' s1 ', ' sname ', [2, 4]][' s1 ', ' sname ', [2, 4]] when a=b is just Jane Single-digit string when B points to an address that a points to when B breaks up b new open memory space stores new worth S=S1 lists and other complex data types that point to a whole address sharing a piece of memory space does not open up new memory space
Import Copys = [' s ', ' sname ', [1, 2]]s1 = ss1[0]= ' s1 ' s[2][0] = 2s1[2][1]=4s3= copy.deepcopy (s) s3[0] = ' S3 ' S3[2][0]=6print ( s) print (S1) print (S3) Output: [' s1 ', ' sname ', [2, 4]][' s1 ', ' sname ', [2, 4]][' S3 ', ' sname ', [6, 4]]
#深层拷贝-----------------Import copycopy.deepcopy (s)-----------------
Python depth Copy