其他分享
首页 > 其他分享> > 四、数据类型 11.切片copy和dict_copy

四、数据类型 11.切片copy和dict_copy

作者:互联网

切片copy和dict_copy

切片
    可变类型数据切片
        copy最外层对象的值,产生新的内存空间,其中的子对象仅被引用,不会copy

    不可变类型数据切片
        无论子对象是否为可变类型,副本对象和子对象都仅被引用,不会被copy

dict.copy()
    dict对象会被copy,dict中容器类型的子对象仅被引用

# 一维list切片
def list_test():
    list1 = [1,2,3,4,5]
    list2 = list1[:]
    print('list1:',list1,id(list1))
    print('list2:',list2,id(list2))
    list1.remove(3)
    print('list1:',list1,id(list1))
    print('list2:',list2,id(list2))
list_test()


list1: [1, 2, 3, 4, 5] 386665697800
list2: [1, 2, 3, 4, 5] 386665698312
list1: [1, 2, 4, 5] 386665697800
list2: [1, 2, 3, 4, 5] 386665698312

# 多维list切片
def lists_test():
    A = [1,2,3]
    B = [4,5,6]
    C = [A,B]
    print('A:',A,id(A))
    print('C:',C,id(C))

    D = C[:]
    print('D:',D,id(D),' D[0]:',D[0],id(D[0]))

    A[1] = 10
    C[1] = 10
    print('A:',A,id(A))
    print('C:',C,id(C))
    print('D:',D,id(D),' D[0]:',D[0],id(D[0]))
lists_test()


A: [1, 2, 3] 386665698312
C: [[1, 2, 3], [4, 5, 6]] 386797148296
D: [[1, 2, 3], [4, 5, 6]] 386797148232  D[0]: [1, 2, 3] 386665698312
A: [1, 10, 3] 386665698312
C: [[1, 10, 3], 10] 386797148296
D: [[1, 10, 3], [4, 5, 6]] 386797148232  D[0]: [1, 10, 3] 386665698312

# 一维tuple切片
def tuple_test():
    tuple1 = (1,2,3,4,5)
    tuple2 = tuple1[:]
    print('tuple1:',tuple1,id(tuple1))
    print('tuple2:',tuple2,id(tuple2))
tuple_test()


tuple1: (1, 2, 3, 4, 5) 447164963240
tuple2: (1, 2, 3, 4, 5) 447164963240

# 多维tuple切片
def tuples_test():
    A = [1,2,3]
    B = (4,5,6)
    C = (A,B)
    print('A:',A,id(A))
    print('B:',B,id(B))
    print('C:',C,id(C))

    D = C[:]
    print('D:',D,id(D),' D[0]:',D[0],id(D[0]))

    A[1] = 10
    print('A:',A,id(A))
    print('C:',C,id(C))
    print('D:',D,id(D),' D[0]:',D[0],id(D[0]))
tuples_test()


A: [1, 2, 3] 447164862984
B: (4, 5, 6) 447295945528
C: ([1, 2, 3], (4, 5, 6)) 447295968328
D: ([1, 2, 3], (4, 5, 6)) 447295968328  D[0]: [1, 2, 3] 447164862984
A: [1, 10, 3] 447164862984
C: ([1, 10, 3], (4, 5, 6)) 447295968328
D: ([1, 10, 3], (4, 5, 6)) 447295968328  D[0]: [1, 10, 3] 447164862984

# dict copy
def dict_test():
    dict1 = {'menu':[1,2,3,4,5],'num':10}
    dict2 = dict1.copy()

    print('dict1:',dict1,id(dict1))
    print('dict2:',dict2,id(dict2))

    # 修改dict
    dict1['name'] = 'messi'
    print('dict1:',dict1,id(dict1))
    print('dict2:',dict2,id(dict2))

    # 修改dict中的value
    dict1['menu'].remove(3)
    dict1['num'] = 100
    print('dict1:',dict1,id(dict1))
    print('dict2:',dict2,id(dict2))
dict_test()


dict1: {'menu': [1, 2, 3, 4, 5], 'num': 10} 570645560920
dict2: {'menu': [1, 2, 3, 4, 5], 'num': 10} 570677005752
dict1: {'menu': [1, 2, 3, 4, 5], 'num': 10, 'name': 'messi'} 570645560920
dict2: {'menu': [1, 2, 3, 4, 5], 'num': 10} 570677005752
dict1: {'menu': [1, 2, 4, 5], 'num': 100, 'name': 'messi'} 570645560920
dict2: {'menu': [1, 2, 4, 5], 'num': 10} 570677005752

标签:11,10,dict1,dict2,list1,数据类型,print,copy,id
来源: https://www.cnblogs.com/HyeJeong/p/15686072.html