python 基础之深浅拷贝

深浅拷贝

s=[[1,2],'fgfgf','cx']
s3=s.copy()
print(s)
print(s3)

  测试

D:\python\python.exe D:/untitled/dir/for.py
[[1, 2], 'fgfgf', 'cx']
[[1, 2], 'fgfgf', 'cx']

Process finished with exit code 0

  浅拷贝之修改

s=[[1,2],'fgfgf','cx']
s3=s.copy()
print(s3)
s3[1]='chhgghg'
print(s)
print(s3)

  测试

D:\python\python.exe D:/untitled/dir/for.py
[[1, 2], 'fgfgf', 'cx']
[[1, 2], 'fgfgf', 'cx']
[[1, 2], 'chhgghg', 'cx']

Process finished with exit code 0

  浅拷贝之修改列表

s=[[1,2],'fgfgf','cx']
s3=s.copy()
print(s3)
s3[0][1]='chhgghg'
print(s)
print(s3)

  测试

D:\python\python.exe D:/untitled/dir/for.py
[[1, 2], 'fgfgf', 'cx']
[[1, 'chhgghg'], 'fgfgf', 'cx']
[[1, 'chhgghg'], 'fgfgf', 'cx']

Process finished with exit code 0

  浅拷贝是复制一层

深拷贝

haha = ['cx',123,[15000,9000]]
wit = copy.deepcopy(haha)
wit[0] = 'cd'
wit[1] = 6666
wit[2][1] = 1999
print(wit)
print(haha)

  测试

D:\python\python.exe D:/untitled/dir/for.py
[[1, 2], 'fgfgf', 'cx']
[[1, 'chhgghg'], 'fgfgf', 'cx']
[[1, 'chhgghg'], 'fgfgf', 'cx']
['cd', 6666, [15000, 1999]]
['cx', 123, [15000, 9000]]

  

猜你喜欢

转载自www.cnblogs.com/rdchenxi/p/11127570.html