zoukankan      html  css  js  c++  java
  • python核心编程笔记1

    浅拷贝

    person = ['name',['savings',100000.00]]
    hubby = person[:]
    wifey = list(person)
    
    hubby[0] = 'ljh'
    wifey[0] = 'yy'
    
    print hubby,wifey
    
    >>['ljh', ['savings', 100000.0]] ['yy', ['savings', 100000.0]]
    
    hubby[1][1] = 50000.00
    print hubby,wifey
    >>['ljh', ['savings', 50000.0]] ['yy', ['savings', 50000.0]]

    深拷贝

    import copy
    
    person = ['name',['savings',100000.00]]
    hubby = person
    wifey = copy.deepcopy(person)
    
    hubby[0] = 'ljh'
    wifey[0] = 'yy'
    
    print hubby,wifey
    
    hubby[1][1] = 50000.00
    print hubby,wifey

    ps:

    1、非容器类型(数字、字符串、原子类对象,比如代码、类型和xrange对象等)没有被拷贝这么一说,浅拷贝是用完全切片操作来完成的。

    2、如果元组只包含原子类对象,那么对它的深拷贝将不会进行,及时操作使用了深拷贝,也只能得到一个浅拷贝。

    import copy
    
    person = ['name',('savings',100000.00)]
    newPerson = copy.deepcopy(person)
    [id(x) for x in person,newPerson]
    [id(x) for x in person]
    [id(x) for x in newPerson]
  • 相关阅读:
    3.3 直方图处理与函数绘图
    光头强
    考试代码模板
    【2015初赛】预备
    NOIP2018 模拟题
    NOIP2017 模拟赛
    【解题报告】树形DP入门
    【解题报告】区间DP
    【解题报告】树形背包
    二分刷题单
  • 原文地址:https://www.cnblogs.com/mohu/p/8198293.html
Copyright © 2011-2022 走看看