Python基础之深浅copy
1. 赋值
```Python
lst1 = [1, 2, 3, ["a", "b", "c"]]
lst2 = lst1
lst1[0] = 11
print(lst1) #[11, 2, 3, ['a', 'b', 'c']]
print(lst2) #[11, 2, 3, ['a', 'b', 'c']]
lst1[3][0] = "d"
print(lst1) #[11, 2, 3, ['d', 'b', 'c']]
print(lst2) #[11, 2, 3, ['d', 'b', 'c']]
对于赋值运算来说,lst1和lst2指向的是同一个内存地址,所以它们是一样的。
<h2>2. 浅copy</h2>
<h3>2.1 示例1</h3>
```Python
lst1 = [1, 2, 3, ["a", "b", "c"]]
lst2 = lst1.copy()
print(lst1, id(lst1))
print(lst2, id(lst2))
执行结果为:
[1, 2, 3, ['a', 'b', 'c']] 48285736
[1, 2, 3, ['a', 'b', 'c']] 48285776
2.2 示例二
```Python
lst1 = [1, 2, 3, ["a", "b", "c"]]
lst2 = lst1.copy()
lst1[0] = 11
print(lst1, id(lst1))
print(lst2, id(lst2))
```
执行结果为:
```Python
[11, 2, 3, ['a', 'b', 'c']] 48285736
[1, 2, 3, ['a', 'b', 'c']] 48285776
```
2.3 示例三
```Python
lst1 = [1, 2, 3, ["a", "b", "c"]]
lst2 = lst1.copy()
lst1[3][0] = "d"
print(lst1, id(lst1))
print(lst2, id(lst2))
```
执行结果为:
```Python
[1, 2, 3, ['d', 'b', 'c']] 48285536
[1, 2, 3, ['d', 'b', 'c']] 48285736
```
2.4 总结
对于浅copy来所,第一层创建的是新的内存地址,而从第二层开始,指向的都是同一个内存地址。
所以,对于第二层以及更深的层数来说,保持一致性。
3. 深拷贝(deepcopy)
3.1 示例一
```Python
import copy
lst1 = [1, 2, 3, ["a", "b", "c"]]
lst2 = copy.deepcopy(lst1)
print(lst1, id(lst1))
print(lst2, id(lst2))
执行结果为:
```Python
[1, 2, 3, ['a', 'b', 'c']] 54249392
[1, 2, 3, ['a', 'b', 'c']] 54249992
3.2 示例二
```Python
import copy
lst1 = [1, 2, 3, ["a", "b", "c"]]
lst2 = copy.deepcopy(lst1)
lst1[0] = 11
print(lst1, id(lst1))
print(lst2, id(lst2))
执行结果为:
```Python
[11, 2, 3, ['a', 'b', 'c']] 48678832
[1, 2, 3, ['a', 'b', 'c']] 48679432
3.3 示例三
```Python
import copy
lst1 = [1, 2, 3, ["a", "b", "c"]]
lst2 = copy.deepcopy(lst1)
lst1[3][0] = "d"
print(lst1, id(lst1))
print(lst2, id(lst2))
执行结果为:
```Python
[1, 2, 3, ['d', 'b', 'c']] 46516144
[1, 2, 3, ['a', 'b', 'c']] 46516744
3.4 总结
对于deepcopy来说,两个是完全独立的,改变任意一个的任何元素(无论多少层),另一个绝对不改变。
Python基础之深浅copy的更多相关文章
- python 集合和深浅copy
#1数据类型的补充#2.集合set#3.深浅copy 补充:str --> bytes s.encode('gbk')bytes --> str s.decode('gbk') 1.数据类 ...
- Python基础—set、copy(Day7)
一.数据类型补充 1.str:.isspace()字符串是空格或至少是一个空格. s='alex' s1=' ' ret=s1.isspace() print(ret)执行结果:True 2.tul ...
- python基础05--深浅copy, set,bytes
1.1 深浅 copy 1. = 赋值操作, lis1=[1,2,3] list2 = list1 list1.append(4) 则list1,list2都变 赋值都指向同一个地址,改变一个 ...
- python基础(6)-深浅拷贝
赋值 字符串和数字 # id()函数可以获取变量在内存中的地址标识 num1 = 2; num2 = 2; print(id(num1)) # result:8791124202560 print(i ...
- python 基础之深浅拷贝
深浅拷贝 s=[[1,2],'fgfgf','cx'] s3=s.copy() print(s) print(s3) 测试 D:\python\python.exe D:/untitled/dir/f ...
- Python基础:深浅拷贝
对于数字.字符串深浅拷贝: import copy num = 0 copy_num = copy.copy(num) print("These are normal copy") ...
- python学习日记(深浅copy)
赋值 #赋值,指向同一内存地址 l1 = [1,2,3,4,5] l2 = l1 print(l1,l2) print(id(l1),id(l2)) 浅copy #浅copy,第一层互相独立,创建了新 ...
- python入门之深浅copy
a1=["a","b","c","aa"] b1=a1 a1[0]=" print(a1,b1) 此时结果为: ...
- python基础七之copy
浅拷贝 没有嵌套,则copy后完全不同,有嵌套,则copy后本体不同,嵌套相同. l1 = [1, 2, [4, 5, 6], 3] l2 = l1.copy() print(l1 is l2) # ...
随机推荐
- 牛客 26C 手铐 (缩环, 树形dp)
先缩环建树, 对于树上个环$x,y$, 假设$x,y$路径上有$cnt$个环(不包括$x,y$), 贡献就为$2^{cnt}$. 这题卡常挺严重的, 刚开始用并查集合并竟然T了. #include & ...
- OSG3.4内置Examples解析【目录】
opengl渲染管线 从整体上解读OpenGL的渲染流程 一 从整体上解读OpenGL的渲染流程 二 osg与animate相关示例解析 OSG3.4内置Examples(osganimate)解析 ...
- C#添加带验证的websevice接口
记录一下,方便下次使用,或者能帮助到别人. 一.添加服务引用,输入WSDL文件地址. 二.代码 public TESTClient TestContext() { var binding = new ...
- 转载博客(Django2.0集成xadmin管理后台遇到的错误)
转载博客地址:https://blog.csdn.net/yuezhuo_752/article/details/87916995 django默认是有一个admin的后台管理模块,但是丑,功能也不齐 ...
- EFcore的 基础理解<三> 多对多,唯一约束
唯一约束,替代键. modelBuilder.Entity<Car>() .HasAlternateKey(c => new { c.State, c.LicensePlate }) ...
- 巧妙记忆 ++i 和 i++ 的区别
区别在于: i++先做别的事,再自己加1, ++i先自己加1,再做别的事情, 形象的理解,你可以把 ++i比作自私的人,首先考虑自己的事, i++是无私的,先为别人照想,这样方便记忆. 示例: a = ...
- 个人学习HTML以及CSS所得体会
拥有自己样式的浏览器: 苹果,欧朋,谷歌,IE,火狐 form标签<form></form> 表单属性: 1,action主要同来规定表单的作用,提交到处理器上面处理URL,默 ...
- 使用LEANGOO泳道
转自:https://www.leangoo.com/leangoo_guide/leangoo_yongdao.html 列表使用纵向的纬度管理卡片,通常代表卡片的工作的不同阶段,或者任务的状态.泳 ...
- element之tree组件样式重写
1.改写实现效果: 2.页面代码 <el-tree :data="data" :props="defaultProps" @node-click=&quo ...
- Oralce问题之Oracle ORA-28001:某用户密码过期
解决办法: (1).通过CMD打开命令行窗口,以sysdba连接数据库 SqlPlus / as sysdba (2).通过查询dba_user检查哪些用户过期 Sql>Select UserN ...