美文网首页
Python深浅拷贝

Python深浅拷贝

作者: 南少cc | 来源:发表于2017-04-17 16:23 被阅读0次

    导入模块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>

    相关文章

      网友评论

          本文标题:Python深浅拷贝

          本文链接:https://www.haomeiwen.com/subject/lrstzttx.html