首页 > 代码库 > python对象的复制问题

python对象的复制问题

 

list 的拷贝问题:
1,
 1 >>> a 2 [1, 2] 3 >>> b=a[:] 4 >>> b 5 [1, 2] 6 >>> b[0]=20 7 >>> b 8 [20, 2] 9 >>> a10 [1, 2]

 

2,
 1 import copy 2 >>> c=copy.copy(a) 3 >>> c 4 [1, 2] 5 >>> c[1]=30 6 >>> c 7 [1, 30] 8 >>> a 9 [1, 2]10 >>> b11 [20, 2]

dictionary:

 1 >>> a=[(he‘,1),(wo‘,jia)] 2 >>> p=dict(a) 3 >>> p 4 {wo‘: jia‘, he‘: 1} 5 >>> b=p.copy() 6 >>> b 7 {wo‘: jia‘, he‘: 1} 8 >>> b[wo‘]=ja 9 >>> b10 {wo‘: ja‘, he‘: 1}11 >>> p12 {wo‘: jia‘, he‘: 1}

python对象的复制问题