• python 数据的拷贝


    # -*- config=utf-8 -*-
    #数据的拷贝
    a=[1,2,3,4,5,6,"a","C"];
    b=a;# a 与 b 的地址空间相同
    a.append("b");
    b.append("f");
    print(b,a);#[1, 2, 3, 4, 5, 6, 'a', 'C', 'b', 'f'] [1, 2, 3, 4, 5, 6, 'a', 'C', 'b', 'f'] 当改变a的时候b也变 改变b a也变
    print(id(b),id(a)); # 11075528 11075528 地址空间相同
    ###############################################
    import copy;
    #拷贝就是对内存中数据的应用
    # 这就是浅拷贝
    list=[1,2,3,4,["a","b","c"]];
    list_1=copy.copy(list);
    print(list,list_1)
    print(id(list),id(list_1)); #11367752 10970440 地址空间不同 彼此的地址空间不同
    list.append("5");
    print(list,list_1);#[1, 2, 3, 4, ['a', 'b', 'c'], '5']  [1, 2, 3, 4, ['a', 'b', 'c']] list 改变 但 list_1 并没有变
    print(id(list[0]),id(list_1[0]));# 1394274096  1394274096 他们各自元素的地址空间还是相同的
    list[4].append("d");
    print(list,list_1);#[1, 2, 3, 4, ['a', 'b', 'c', 'd'], '5']  [1, 2, 3, 4, ['a', 'b', 'c', 'd']] 都变了
    
    # 深拷贝
    list_2=[1,2,3,4,["a","b","c"]];
    list_3=copy.deepcopy(list_2);
    print(id(list_2),id(list_3));#17402824 17402248 地址不同
    print(id(list_2[4]),id(list_3[4])); #11241032 11242056  地址不同
    list_2[4].append("d");
    print(list_2,list_3);#[1, 2, 3, 4, ['a', 'b', 'c', 'd']] [1, 2, 3, 4, ['a', 'b', 'c']]  list_3 没改变
  • 相关阅读:
    Android中实现ListView圆角效果[转]
    移动终端开发必备知识【转】
    android-supporting-multiple-devices
    Android @+id与@id的区别
    loading android
    Loading Image
    合理的薪酬策略——揭秘万达电商(3)
    Node.js的helloworld 程序
    codeforces Gravity Flip 题解
    HDU 3853 向下向右找出口问题-期望dp
  • 原文地址:https://www.cnblogs.com/jalja/p/5609643.html
Copyright © 2020-2023  润新知