浅拷贝

图片里黄色块:这是lst列表的内存地址【字符串在列表中:同样的字符内存地址一样嘛】

浅拷贝_第1张图片

浅拷贝

print("浅拷贝")
import copy

lst = ['str2', 'str3', 'str4']
sourcelst = ['str1', 'str2', 'str3', lst]
copylst = copy.copy(sourcelst)
print("--->【*】sourcelst: ",id(sourcelst), sourcelst)
print("***>【*】copylst  : ",id(copylst), copylst)
print("+++>【*】lst      : ",id(lst), lst)
print("--->sourcelst id:",[id(ele) for ele in sourcelst])
print("***>copylst   id: ",[id(ele) for ele in copylst])
print("+++>lst       id: ",[id(ele) for ele in lst])

print("\n1.当sourceLst列表发生变化,copyLst中存储的lst内存地址没有改变")
sourcelst.append('source')
copylst.append('copy')
print("\t--->添加【source】sourcelst: ",id(sourcelst), sourcelst)
print("\t***>添加【copy】copylst    : ",id(copylst), copylst)
print("\t--->sourcelst id:",[id(ele) for ele in sourcelst])
print("\t***>copylst   id:",[id(ele) for ele in copylst])

print("\n2.sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址,所以copyLst不会发生改变。")
sourcelst[0] = 'change'
print("\t--->修改【[0]=change】sourcelst: ",id(sourcelst), sourcelst)
print("\t***>                copylst    : ",id(copylst), copylst)
print("\t--->sourcelst id:",[id(ele) for ele in sourcelst])
print("\t***>copylst   id:",[id(ele) for ele in copylst])
 
print("\n3.以当lst发生改变的时候,sourceLst和copyLst两个列表就都发生了改变。")
lst.append('Append')
print("\t--->【*】sourcelst: ",id(sourcelst), sourcelst)
print("\t***>【*】copylst  : ",id(copylst), copylst)
print("\t+++>【*】lst      : ",id(lst), lst)
print("\t--->sourcelst id:",[id(ele) for ele in sourcelst])
print("\t***>copylst   id: ",[id(ele) for ele in copylst])
print("\t+++>lst       id: ",[id(ele) for ele in lst])

输出

浅拷贝
--->*】sourcelst:  2570876328328 ['str1', 'str2', 'str3', ['str2', 'str3', 'str4']]
***>*】copylst  :  2570876329544 ['str1', 'str2', 'str3', ['str2', 'str3', 'str4']]
+++>*】lst      :  2570876328520 ['str2', 'str3', 'str4']
--->sourcelst id: [2570876140824, 2570876139984, 2570876141048, 2570876328520]
***>copylst   id:  [2570876140824, 2570876139984, 2570876141048, 2570876328520]
+++>lst       id:  [2570876139984, 2570876141048, 2570876463232]

1.当sourceLst列表发生变化,copyLst中存储的lst内存地址没有改变
	--->添加【source】sourcelst:  2570876328328 ['str1', 'str2', 'str3', ['str2', 'str3', 'str4'], 'source']
	***>添加【copy】copylst    :  2570876329544 ['str1', 'str2', 'str3', ['str2', 'str3', 'str4'], 'copy']
	--->sourcelst id: [2570876140824, 2570876139984, 2570876141048, 2570876328520, 2570832868496]
	***>copylst   id: [2570876140824, 2570876139984, 2570876141048, 2570876328520, 2570832271768]

2.sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址,所以copyLst不会发生改变。
	--->修改【[0]=change】sourcelst:  2570876328328 ['change', 'str2', 'str3', ['str2', 'str3', 'str4'], 'source']
	***>                copylst    :  2570876329544 ['str1', 'str2', 'str3', ['str2', 'str3', 'str4'], 'copy']
	--->sourcelst id: [2570837371232, 2570876139984, 2570876141048, 2570876328520, 2570832868496]
	***>copylst   id: [2570876140824, 2570876139984, 2570876141048, 2570876328520, 2570832271768]

3.以当lst发生改变的时候,sourceLst和copyLst两个列表就都发生了改变。
	--->*】sourcelst:  2570876328328 ['change', 'str2', 'str3', ['str2', 'str3', 'str4', 'Append'], 'source']
	***>*】copylst  :  2570876329544 ['str1', 'str2', 'str3', ['str2', 'str3', 'str4', 'Append'], 'copy']
	+++>*】lst      :  2570876328520 ['str2', 'str3', 'str4', 'Append']
	--->sourcelst id: [2570837371232, 2570876139984, 2570876141048, 2570876328520, 2570832868496]
	***>copylst   id:  [2570876140824, 2570876139984, 2570876141048, 2570876328520, 2570832271768]
	+++>lst       id:  [2570876139984, 2570876141048, 2570876463232, 2570876464520]

你可能感兴趣的:(Python)