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) # ...
随机推荐
- k8s-helm安装
kubernetes 1.15安装部署helm插件 简单介绍: Helm其实就是一个基于Kubernetes的程序包(资源包)管理器,它将一个应用的相关资源组织成为Charts,并通过Charts ...
- 怎样查看 MySQL 版本号
1. 在命令行中直接查看版本号 mysql -V 2. 在 mysql --help 中查找与版本相关的信息 mysql --help | grep Ver 3. 在mysql命令行里面查看版本信息 ...
- Typeof() 和 GetType()区别
1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...
- JS基础_函数的简介
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [转载]Java序列化与反序列化
[转载]Java序列化与反序列化 来源: https://www.cnblogs.com/anitinaj/p/9253921.html 序列化和反序列化作为Java里一个较为基础的知识点,那你能说一 ...
- Linux I2C核心、总线和设备驱动
目录 更新记录 一.Linux I2C 体系结构 1.1 Linux I2C 体系结构的组成部分 1.2 内核源码文件 1.3 重要的数据结构 二.Linux I2C 核心 2.1 流程 2.2 主要 ...
- C++ STL 之 map
#include <iostream> #include <map> using namespace std; // map构造函数 // map<T1, T2> ...
- oracel数据泵导出导入
Oracle11g 使用数据泵导入/导出数据 expdp/impdp 目标:使用oracle数据泵,将A电脑上的数据库databaseA导出后,再导入到B电脑上的数据库databaseB中. A电脑上 ...
- java web开发跨域问题
分布式环境,前后端分离背景下跨域问题 1.1 设置页面document.domain去把2个页面之间的跨域交互统一 一级域名相同的情况下 调用者和页面提供者进行一个协调 页面提供者要在document ...
- 【异常】 Ensure that config phoenix.schema.isNamespaceMappingEnabled is consistent on client and server.
1 详细异常 ror: ERROR 726 (43M10): Inconsistent namespace mapping properties. Ensure that config phoenix ...