zoukankan      html  css  js  c++  java
  • python中的浅拷贝与深拷贝

    浅拷贝可以拿丈夫与媳妇公用一张银行卡来举例  
    # python >>> husband = ['liang',123,[10000,6000]] #丈夫的银行卡信息(姓名,id,额度,余额) >>> wife = husband.copy() #媳妇和丈夫公用一张银行卡(姓名与id不同,但是额度与余额相同) >>> wife[0] = 'yue' >>> wife[1] = 456 >>> wife[2][1] -= 3000 #媳妇花费3000 >>> wife ['yue', 456, [10000, 3000]] #显示媳妇余额 >>> husband ['liang', 123, [10000, 3000]] #显示丈夫余额

      

    浅拷贝只是拷贝第一层,深拷贝相当于克隆,深拷贝如下:

    引入了小三,小三花费的钱媳妇不知道:
    
    >>> liang = ['xin',111,[15000,10000]]
    >>> yue = liang.copy()
    >>> yue
    ['xin', 111, [15000, 10000]]
    >>> yue[0] = 'mumu'
    >>> yue[1] = 222
    >>> yue[2][1] -= 5000
    >>> yue
    ['mumu', 222, [15000, 5000]]
    >>> liang
    ['xin', 111, [15000, 5000]]
    以上是浅拷贝;
    
    以下是深拷贝:
    >>> import copy
    >>> xiaosan = copy.deepcopy(liang)
    >>> xiaosan[0] ="**"
    >>> xiaosan[1] = 333
    >>> xiaosan[2][1] -= 1999
    >>> xiaosan
    ['**', 333, [15000, 3001]]            #已经减少啦
    >>> liang
    ['xin', 111, [15000, 5000]]
    >>> yue
    ['mumu', 222, [15000, 5000]]       #媳妇不知道
    

      

  • 相关阅读:
    ls: cannot open directory .: Permission denied
    postman报错:Could not get any response
    IO流的继承关系
    吸顶效果
    linux报错:ls command not found
    内存寻址能力与CPU的位宽有关系吗?
    CPU与内存
    SQL语言的分类
    32位CPU和64位CPU 区别
    linux添加环境变量
  • 原文地址:https://www.cnblogs.com/lixinliang/p/8370931.html
Copyright © 2011-2022 走看看