Python深浅拷贝

2017-04-17  本文已影响0人  南少cc

导入模块import copy

<pre>names = ['lilei','hanmei',['lll','rrrr']]

深拷贝

names2 = copy.deepcopy(names)

浅拷贝2中方式 只拷贝一层

1

names2 = copy.copy(names)

2

names2 = names[:]

names[1] = 'HANMEI'
names[2][0] = 'KKKKK'
print(names)
print(names2)</pre>

浅拷贝打印:

<pre>['lilei', 'HANMEI', ['KKKKK', 'rrrr']]
['lilei', 'hanmei', ['KKKKK', 'rrrr']]</pre>

深拷贝打印:

<pre>['lilei', 'HANMEI', ['KKKKK', 'rrrr']]
['lilei', 'hanmei', ['lll', 'rrrr']]</pre>

上一篇下一篇

猜你喜欢

热点阅读