Python3 复制和深浅copy
赋值:
列表的赋值:
1 list1 = ['peter','sam'] 2 list2 = list1 3 4 print(list1,id(list1)) 5 print(list2,id(list2)) 6 list1.append('hery') 7 print(list1,id(list1)) 8 print(list2,id(list2)) 9 10 ['peter', 'sam'] 5071496 11 ['peter', 'sam'] 5071496 12 ['peter', 'sam', 'hery'] 5071496 13 ['peter', 'sam', 'hery'] 5071496
字典的赋值:
1 dic = {'name':'just'} 2 dic1 = dic 3 4 print(dic,id(dic)) 5 print(dic1,id(dic1)) 6 7 dic['age'] = 27 8 9 print(dic,id(dic)) 10 print(dic1,id(dic1)) 11 12 13 14 15 {'name': 'just'} 38505064 16 {'name': 'just'} 38505064 17 {'name': 'just', 'age': 27} 38505064 18 {'name': 'just', 'age': 27} 38505064
View Code
字符串赋值:
1 str = 'hello' 2 str1 = str 3 4 print(str,id(str)) 5 print(str1,id(str1)) 6 7 str = str.replace('e','E') 8 9 print(str,id(str)) 10 print(str1,id(str1)) 11 12 13 14 hello 35026176 15 hello 35026176 16 hEllo 39638608 17 hello 35026176
View Code
创建两个相同的变量,他们的内存地址相同(以前版本好像是不同)
1 str = 'h' 2 str3 = 'h' 3 4 print(str,id(str)) 5 print(str3,id(str3)) 6 print( str is str3) 7 8 9 10 h 39056528 11 h 39056528 12 True
View Code
浅copy:
浅copy来说,第一层创建的是新的内存地址。而从第二层开始,指向的是同一个内存地址,所有,对于第二层以及更深的层数来说,保持一致性。
1 just = ['eric','bob',34,'ida'] 2 dep = ['helo','welcome','jams'] 3 jesp = just.copy() 4 5 print(just,id(just)) 6 print(jesp,id(jesp)) 7 8 9 10 ['eric', 'bob', 34, 'ida'] 37435528 11 ['eric', 'bob', 34, 'ida'] 37497480
View Code
1 just = ['eric','bob',34,'ida'] 2 dep = ['helo','welcome','jams'] 3 just.append(dep) 4 jesp = just.copy() 5 just[1] = 'tom' 6 jesp[4][0] = 'hi' 7 8 print(just,id(just)) 9 print(jesp,id(jesp)) 10 11 12 13 ['eric', 'tom', 34, 'ida', ['hi', 'welcome', 'jams']] 42744008 14 ['eric', 'bob', 34, 'ida', ['hi', 'welcome', 'jams']] 43021128
View Code
深copy:
对深copy来说,两个是完全独立的,改变任意一个元素(无论是多少层),另一个不会随着改变。
1 import copy 2 just = ['eric','bob',34,'ida'] 3 dep = ['helo','welcome','jams'] 4 just.append(dep) 5 jesp = copy.deepcopy(just) 6 just[1] = 'tom' 7 jesp[4][0] = 'hi' 8 9 print(just,id(just)) 10 print(jesp,id(jesp)) 11 12 13 14 ['eric', 'tom', 34, 'ida', ['helo', 'welcome', 'jams']] 43071752 15 ['eric', 'bob', 34, 'ida', ['hi', 'welcome', 'jams']] 43348872
转载于:https://www.cnblogs.com/occl/p/9018540.html
Python3 复制和深浅copy相关推荐
- python3 copy_python3 深浅copy对比详解
一.赋值对比 1.列表 l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) #[1, 2, 3, 'a'] [1, 2, 3, 'a'] print(id ...
- python3 copy_Python3数据的深浅copy和赋值 | 吴老二
在没有说深浅copy之前,先说一下变量.变量是的数据类型取决于变量的值,变量原本没有值,赋值之后的值是什么数据类型,变量就是什么数据类型.变量的值存在内存中,变量的作用就是通过变量名查找变量值.举个例 ...
- Python_赋值和深浅copy
Python中赋值和深浅copy存储变化 在刚学python的过程中,新手会对python中赋值.深浅copy的结果感到丈二和尚.经过多次在网上查找这几个过程的解释说明以及实现操作观察,初步对这几个过 ...
- Python基础三--字典,集合,编码,深浅copy,元祖、文件操作
字典 dict数据类型划分:可变数据类型,不可变数据类型不可变数据类型: 元组,bool值,int,str 可哈希可变数据类型: list,dict,set 不可哈希 dict key :必须是不可变 ...
- python怎么设置颜色深浅变化_Python赋值、深浅copy
Python赋值.深浅copy assignment: 在 Python 中,对象的赋值就是简单的对象引用,这点和 C++不同,如下所示 a = [1, 2, 'hello', ['python', ...
- python 的内存回收,及深浅Copy详解
一.python中的变量及引用 1.1 python中的不可变类型: 数字(num).字符串(str).元组(tuple).布尔值(bool<True,False>) 接下来我们讲完后你就 ...
- [转载] python创建集合、计算a|b_python之路(集合,深浅copy,基础数据补充)
参考链接: Python 集合set | symmetric_difference 一.集合:类似列表,元组的存储数据容器,不同点是不可修改,不可重复.无序排列. 1.创建集合: (1).set1 = ...
- 乐学python怎么样_铁乐学Python_day07_集合and浅谈深浅copy
1.[List补充] 在循环一个列表时,最好不要使用元素和索引进行删除操作,一旦删除,索引会随之改变,容易出错. 如果想不出错,可以采用倒着删除的方法,因为倒着删除进行的话,只是后面元素的位置发生了变 ...
- python之深浅copy
python之深浅copy 文章目录 python之深浅copy 引入 1.为什么要使用深浅拷贝 一.赋值运算 1.赋值运算原理 2.示例 3.总结 二.拷贝 三.浅拷贝 什么是浅拷贝? 1.浅拷贝原 ...
最新文章
- ViewPager 实现界面加载不同的数据
- java action的头文件注释_有没有java大神,帮我详细注释下面的代码,action里面复制来的...
- python知识:如何从图片的四周扩大一些尺寸
- FIR调用DSP48E_05
- java aop设计_Spring4.x基础配置(三):Spring AOP
- windows批处理运行java程序
- 中国女子高尔夫球场市场趋势报告、技术动态创新及市场预测
- 大数据之有指导数据挖掘方法的模型
- linux之菜鸟学习第一天
- angular1的复选框指令--checklistModel
- python 代码行数统计工具_Python实现一个代码行数统计工具
- 方程从pdf转word_在Word中创建化学方程式和图表
- 反馈抑制器使用场景与市场
- Moto me525+ 刷机
- 从写作到演讲,虾米君不断尝试的 2021|年终回顾
- 高数 | 【重积分】线面积分880例题
- 新闻资讯android版
- 【数分】7. AB实验篇
- Nginx + uWSGI + Python + Django构建必应高清壁纸站
- stm32开发之使用Keil MDK以及标准外设库创建STM32工程