python 深浅拷贝

深浅拷贝

#对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址
    import copy
    a1 = 22255
    a2 = 22255
    print(id(a1),id(a2)) #3428240 3428240

#对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。

    import copy
    #字典
    n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
    ##赋值
    n2 = n1
    print(n1,n2) #{'k1': 'wu', 'k2': 123, 'k3': ['alex', 456]} {'k1': 'wu', 'k2': 123, 'k3': ['alex', 456]}
    print(id(n1),id(n2))  #6674440 6674440 #内存地址一样
    ##浅拷贝
    n3 = copy.copy(n1)
    print(n1,n3) #{'k1': 'wu', 'k2': 123, 'k3': ['alex', 456]} {'k1': 'wu', 'k2': 123, 'k3': ['alex', 456]}
    print(id(n1),id(n3)) #6936584 12067848  #浅拷贝第一级,内存地址相同
    print(id(n1['k3']),id(n3['k3'])) #18741768 18741768
    ##深拷贝
    n4 = copy.deepcopy(n1)
    print(n1,n4) #{'k3': ['alex', 456], 'k2': 123, 'k1': 'wu'} {'k3': ['alex', 456], 'k1': 'wu', 'k2': 123}
    print(id(n1),id(n4)) #6805512 11736904
    print(id(n1['k3']),id(n4['k3'])) #7601032 7599496 #深拷贝第二级,内存地址也不相同
    
    #列表
    n1 = [1,2,3,4,5,[6,7],]
    ##赋值
    n2 = n1
    print(n1,n2) #[1, 2, 3, 4, 5, [6, 7]] [1, 2, 3, 4, 5, [6, 7]]
    print(id(n1),id(n2)) #18609928 18609928
    print(id(n1[5]),id(n2[5])) #18609544 18609544
    ##浅拷贝
    n3 = copy.copy(n1)
    print(n1,n3) #[1, 2, 3, 4, 5, [6, 7]] [1, 2, 3, 4, 5, [6, 7]]
    print(id(n1),id(n3)) #18609928 18232904
    print(id(n1[5]),id(n3[5])) #18609544 18609544
    ##深拷贝
    n4 = copy.deepcopy(n1)
    print(n1,n4) #[1, 2, 3, 4, 5, [6, 7]] [1, 2, 3, 4, 5, [6, 7]]
    print(id(n1),id(n4)) #18609928 18611848
    print(id(n1[5]),id(n4[5])) #18609544 18611912


    #元组
    一个小插曲:
        import copy
        n1 = (1,2,3,4,5,(6,7,),)
        #赋值
        n2 = n1
        print('n1:',n1,'n2:',n2) #n1: (1, 2, 3, 4, 5, (6, 7)) n2: (1, 2, 3, 4, 5, (6, 7))
        print(id(n1),id(n2)) #10416584 10416584
        print(id(n1[5]),id(n2[5])) #18415304 18415304
        print(type(n1),type(2)) #<class 'tuple'> <class 'int'>
        #浅拷贝
        n3 = copy.copy(n1)
        print('n1:',n1,'n3:',n3) #n1: (1, 2, 3, 4, 5, (6, 7)) n2: (1, 2, 3, 4, 5, (6, 7))
        print(id(n1),id(n3)) #10416584 10416584
        print(id(n1[5]),id(n3[5])) #18415304 18415304
        print(type(n1),type(3)) #<class 'tuple'> <class 'int'>
        #深拷贝
        n4 = copy.deepcopy(n1)
        print('n1:',n1,'n4:',n4) #n1: (1, 2, 3, 4, 5, (6, 7)) n2: (1, 2, 3, 4, 5, (6, 7))
        print(id(n1),id(n4)) #10416584 10416584
        print(id(n1[5]),id(n4[5])) #18415304 18415304
        print(type(n1),type(5)) #<class 'tuple'> <class 'int'>

python 深浅拷贝_第1张图片

    再一个小插曲:
        import copy
        n1 = (1,2,3,4,5,[6,7,],)
        #赋值
        n2 = n1
        print('n1:',n1,'n2:',n2) #(1, 2, 3, 4, 5, [6, 7]) n2: (1, 2, 3, 4, 5, [6, 7])
        print(id(n1),id(n2)) #11465160 11465160
        print(id(n1[5]),id(n2[5])) #18480456 18480456
        print(type(n1),type(2)) #<class 'tuple'> <class 'int'>
        #浅拷贝
        n3 = copy.copy(n1)
        print('n1:',n1,'n3:',n3) #n1: (1, 2, 3, 4, 5, [6, 7]) n3: (1, 2, 3, 4, 5, [6, 7])
        print(id(n1),id(n3)) #11465160 11465160
        print(id(n1[5]),id(n3[5])) #18480456 18480456
        print(type(n1),type(3)) #<class 'tuple'> <class 'int'>
        #深拷贝
        n4 = copy.deepcopy(n1)
        print('n1:',n1,'n4:',n4) #n1: (1, 2, 3, 4, 5, [6, 7]) n4: (1, 2, 3, 4, 5, [6, 7])
        print(id(n1),id(n4)) #11465160 18109736
        print(id(n1[5]),id(n4[5])) #18480456 18478920
        print(type(n1),type(5)) #<class 'tuple'> <class 'int'>

python 深浅拷贝_第2张图片
出现以上问题有可能跟下面的说法有关:
python 深浅拷贝_第3张图片

案例代码

    import copy
    dic = {
        "cpu":[80,],
        "mem":[80,],
        "disk":[80,],
    }
    
    print("old:",dic)
    new_dic1 = copy.copy(dic)
    new_dic1["cpu"][0] = 50
    print("old:",dic)
    print("浅拷贝:",new_dic1)
    #返回结果:
    #old: {'disk': [80], 'cpu': [80], 'mem': [80]}
    #浅拷贝: {'disk': [80], 'cpu': [50], 'mem': [80]}
    
    new_dic2 = copy.deepcopy(dic)
    new_dic2["cpu"][0] = 60
    print("old:",dic)
    print("深拷贝:",new_dic2)
    #返回结果
    # old: {'mem': [80], 'cpu': [50], 'disk': [80]}
    # 深拷贝: {'mem': [80], 'cpu': [60], 'disk': [80]}

你可能感兴趣的:(python 深浅拷贝)