标签:lap hid 赋值 color deepcopy 分享图片 div alt str
赋值:
a=[1,2,3,[4,5]] b=a print(a,id(a))#[1, 2, 3, [4, 5]] 1807996384072 print(b,id(b))#[1, 2, 3, [4, 5]] 1807996384072 a.pop() print(a,id(a))#[1, 2, 3] 1807996384072 print(b,id(b))#[1, 2, 3] 1807996384072
浅拷贝:
l1=[1,2,3,[‘barry‘,‘alex‘]] l2=l1.copy() print(l1,id(l1))#[1, 2, 3, [‘barry‘, ‘alex‘]] 2578599060616 print(l2,id(l2))#[1, 2, 3, [‘barry‘, ‘alex‘]] 2578599889480 l1.append(‘wusir‘) print(l1,id(l1))#[1, 2, 3, [‘barry‘, ‘alex‘], ‘wusir‘] 2578599060616 print(l2,id(l2))#[1, 2, 3, [‘barry‘, ‘alex‘]] 2578599889480 l1[0]=333 print(l1,id(l1))#[333, 2, 3, [‘barry‘, ‘alex‘]] 2578599060616 print(l2,id(l2))#[1, 2, 3, [‘barry‘, ‘alex‘]] 2578599889480 内存地址不同,被拷贝的对象作出改变,另一个不改变
深拷贝:
import copy l1=[1,2,3,[‘barry‘,‘alex‘]] l2=copy.deepcopy(l1) print(l1,id(l1))#[1, 2, 3, [‘barry‘, ‘alex‘]] 2037159109640 print(l2,id(l2))#[1, 2, 3, [‘barry‘, ‘alex‘]] 2037157644168 l1[1]=222 print(l1,id(l1))#[1, 222, 3, [‘barry‘, ‘alex‘]] 2037159109640 print(l2,id(l2))#[1, 2, 3, [‘barry‘, ‘alex‘]] 2037157644168
标签:lap hid 赋值 color deepcopy 分享图片 div alt str
原文地址:https://www.cnblogs.com/li-jj/p/9629884.html