• Python自学之乐-浅析Python的深浅拷贝


    import copy
    #浅拷贝
    person = ["name",["saving",1000]]
    p1 = copy.copy(person)
    p2 = person.copy() #两种都是浅拷贝
    print(person,p1,p2)
    ###结果为 ['name', ['saving', 1000]] ['name', ['saving', 1000]] ['name', ['saving', 1000]]
    person[0] = "clark"
    print(person,p1,p2)
    ###结果为['clark', ['saving', 1000]] ['name', ['saving', 1000]] ['name', ['saving', 1000]] 单个字符未改变
    person[1][1] = 2000
    print(person,p1,p2)#记住这两种方式的最后结果
    ####结果为['clark', ['saving', 2000]] ['name', ['saving', 2000]] ['name', ['saving', 2000]] 列表值改变,对应浅拷贝里的列表值也相应改变

    p1[0] = "徐宏彪"
    print(person,p1,p2)
    ##结果为['clark', ['saving', 2000]] ['徐宏彪', ['saving', 2000]] ['name', ['saving', 2000]]
    p1[1][1] = 3000
    print(person,p1,p2)
    ##结果为['clark', ['saving', 3000]] ['徐宏彪', ['saving', 3000]] ['name', ['saving', 3000]]   结论同上

    #深拷贝
    info  = ["name",["love",20]]
    info1 = copy.deepcopy(info)
    info2 = copy.deepcopy(info)
    print(info,info1,info2)
    info[0] = "clark"
    print(info,info1,info2)
    info[1][1] = 30
    print(info,info1,info2)
    info1[1][1] = 40
    print(info,info1,info2)#深拷贝,各自为独立 所以不管怎么修改任意一个,其他的June不变






  • 相关阅读:
    0019. Remove Nth Node From End of List (M)
    0018. 4Sum (M)
    0278. First Bad Version (E)
    0273. Integer to English Words (H)
    0017. Letter Combinations of a Phone Number (M)
    0016. 3Sum Closest (M)
    0015. 3Sum (M)
    软件测试常见面试题
    如何快速掌握DDT数据驱动测试?
    selenium--三种等待方式
  • 原文地址:https://www.cnblogs.com/clarkxhb/p/7237241.html
Copyright © 2020-2023  润新知