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]
  • 相关阅读:
    struts2上传下载
    git教程
    mysql触发器2
    mysql触发器
    mysql set sql_mode 1055 报错
    一些乱七八糟的话
    linux 命令2
    linux命令 mysql
    东南亚之行(越南篇)
    flume常见配置
  • 原文地址:https://www.cnblogs.com/mohu/p/8198293.html
Copyright © 2011-2022 走看看