问题概述: 在码代码中,需要保存一个字典,用的update,后来发现update的值会随着原字典值得变化而变化。

而后使用deepcopy来保存字典。

update


a = {1:{2:3}}
b= {}
b.update(a)
print(b,1)
a[1][2] = 4
print(b,2)
print(id(a[1]),3)
print(id(b[1]),3)
#结果:
# {1: {2: 3}} 1
# {1: {2: 4}} 2
# 5095520 3
# 5095520 3
#发现这两个前套字典中的内存地址是一个,所以改变一个另一个也改变
 

deepcopy

#deepcopy
from copy import deepcopy
c = {1:{2:3}}
d = deepcopy(c)
print(d,1)
c[1][2] = 4
print(d,2)
print(id(c[1]),3)
print(id(d[1]),3)
#结果
# {1: {2: 3}} 1
# {1: {2: 3}} 2
# 5750192 3
# 54535776 3
#这里发现两个id是不一样的

python3 字典update与deepcopy的更多相关文章

  1. Python3 字典 update() 方法

     Python3 字典 描述 Python 字典 update() 函数把字典dict2的键/值对更新到dict里. 语法 update()方法语法: dict.update(dict2) 参数 di ...

  2. Python3字典update()方法

    描述 Python字典update()函数把字典参数dict2的key/value(键/值)对更新到字典dict里. update()方法语法: dict.update(dict2) 参数 dict2 ...

  3. python3 字典常见用法总结

    python3 字典常见用法总结 Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字典也被称作关联数组或哈希表 ...

  4. Python 字典 update() 方法

    描述 Python 字典 update() 方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中. 用法与 Python dict() 函数相似. 语法 update ...

  5. Python3 字典(map)

    ayout: post title: Python3 字典(map) author: "luowentaoaa" catalog: true tags: mathjax: true ...

  6. Python3字典与集合

    一.Python3字典 字典是另一种可变容器模型,且可存储任意类型对象字典的每个键值(key=>value)对用冒号":"分割,每个键值对之间用逗号"," ...

  7. Python3 字典 get() 方法

     Python3 字典 描述 Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  8. Python3 字典 fromkeys()方法

     Python3 字典 描述 Python 字典 fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...

  9. Python3 字典 pop() 方法

     Python3 字典 描述 Python 字典 pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值.key值必须给出. 否则,返回default值. 语法 pop()方法语法: ...

随机推荐

  1. DedeCms 数据库类使用实例说明 mysql.php

    //dedecms的数据库操作类说明,非常实用,在二次开发中尤其重要.//引入common.inc.php文件require_once (dirname(__FILE__) . "/incl ...

  2. cordova插件新的窗口实例打开连接: cordova-plugin-inappbrowser

    1. 添加插件:cordova plugin add cordova-plugin-inappbrowser  : 2. InAppBrowser可以使用新的窗口实例打开连接,提供了地址栏的显示隐藏, ...

  3. day-14带参装饰器、迭代器

    带参装饰器 通常,装饰器为被装饰的函数添加新功能,需要外界的参数  -- outer参数固定一个,就是func -- inner参数固定同被装饰的函数,也不能添加新参数 -- 可以借助函数的嵌套定义, ...

  4. LVS详细介绍以及遇到的坑

    LVS详细介绍以及遇到的坑 一,概述 本文介绍了我搭建LVS集群的步骤,并且在使用LVS(Linux Virtual Server)过程中遇到的问题和坑, 二,LVS简单介绍 大家都知道,LVS中文意 ...

  5. imp 导入以及换用户报错

    数据库导入操作:SQL> create user user identified by passwd; SQL> create tablespace user datafile '/dat ...

  6. 多线程shell脚本检测主机存活

    局域网中分了很多网段,而IP地址使用情况也未知,前期也没有规划和记录,当新的主机需要使用固定IP的时候,能否第一时间知道哪个IP空闲就显得很重要了,如果一个一个去ping的话太浪费时间. 这里分享一个 ...

  7. ASP.NET: Cookie会话丢失,Session超时配置

    问题描述: asp.net应用中web.config的SessionState节点:原先是 <sessionState mode="InProc" timeout=" ...

  8. CentOS 7中允许远程连接mariadb数据库

    # /etc/init.d/mysql restart 或者 service mysqld start 启动服务 # /etc/init.d/mysql stop 或者 service mysqld ...

  9. JavaScript 函数调用和this指针

    函数调用和this指针 1. 全局环境的this指针 浏览器全局环境下this指向window对象 console.log(this); //Window {postMessage: ƒ, blur: ...

  10. SpringBoot启动源码探究---getRunListener()

    该方法目的是获取SpringApplicationRunListener getRunListener()-----调用----> getSpringFactoriesInstances()-- ...