zoukankan      html  css  js  c++  java
  • 深浅拷贝

    from copy import copy, deepcopy
    l1 = [1,[2,3,4],{20,40,89},4,5]

    赋值

    l2 = l1

    l2[0]=999

    print('l1:')

    print(l1)

    print('l2:')

    print(l2)

    l2[1][0]=898

    print('l1:')

    print(l1)

    print('l2:')

    print(l2)

    浅拷贝

    l3 = copy(l1)

    l3[0] = 786

    print('l1:')

    print(l1)

    print('l3:')

    print(l3)

    结果是

    l1:[1, [2, 3, 4], {40, 89, 20}, 4, 5]

    l3:[786, [2, 3, 4], {40, 89, 20}, 4, 5]

    (符合预期)

    l3[1][0] = 9804

    print('l1:')

    print(l1)

    print('l3:')

    print(l3)

    结果是

    l1:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]

    l3:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]

    (不符合预期)

    l3[1][0] = 9804

    print('l1:')

    print(l1)

    print('l3:')

    print(l3)

    结果是

    l1:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]

    l3:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]

    深拷贝

    l3 = deepcopy(l1)
    l3[1][0] = 9804
    print('l1:')
    print(l1)
    print('l3:')
    print(l3)

    结果是

    l1:[1, [2, 3, 4], {40, 89, 20}, 4, 5]

    l3:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]

    (符合预期)

  • 相关阅读:
    近期学习情况
    java连接数据库的两种方法总结
    近两个星期学习成果
    云笔记第一阶段总结
    圆面积
    C++计算器项目的初始部分
    C++视频课程
    A+B Format
    大一下学期的自我目标
    Kohana的请求流
  • 原文地址:https://www.cnblogs.com/PowerTips/p/11928314.html
Copyright © 2011-2022 走看看