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) # ...
随机推荐
- cygwin gcc 编译windowsAPI 报错的一个解决方案
一开始按照linux的习惯去编译一个使用了windowsAPI的程序 结果提示: $ i686-pc-cygwin-g++ screen_catch.cscreen_catch.c: In funct ...
- Nginx安装启动过程报错libpcre.so.1 cannot open shared object file: No such file or directory
具体报错信息如下: nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: ...
- Java语言中:float、double数据类型在内存中是如何存储的
引用参考 https://www.cnblogs.com/chenmingjun/p/8415464.html#4291528 https://blog.csdn.net/yansmile1/arti ...
- UPX编译及so加固
UPX编译及so加固 来源 https://www.cnblogs.com/Reverser/p/5778042.html 参考 http://www.cnblogs.com/fishou/p/420 ...
- 安装笔记, caffe 、 opencv等
1. 1.1 opencv static linux mkdir build & cd build cmake .. -LH 这句话用来查看编译选项 如果不知道编译啥 可以用这个查看一下 ...
- 【QT 学习笔记】 一、 VS2015+ QT环境安装
1. 安装 qt-opensource-windows-x86-msvc2015_64-5.6.0.exe (根据自己的VS版本来安装) 下载地址 http://download.qt. ...
- scrapy增量爬取
开始接触爬虫的时候还是初学Python的那会,用的还是request.bs4.pandas,再后面接触scrapy做个一两个爬虫,觉得还是框架好,可惜都没有记录都忘记了,现在做推荐系统需要爬取一定的 ...
- xss part2
0x01 xss challenge level 6-10 1.1 level 6 test with typical, notice the script has changed change sc ...
- ESP8266 AT指令
无线网络名称 ESP_XXXXXX,后面的数字是MAC地址后几位 应用模式: AT+CWMODE? //查询 AT+CWMODE=<mode> //设置(重启后生效).1-Station模 ...
- ContextMenu菜单创建 上下文菜单的基本认识q
MainActivity.class public class MainActivity extends AppCompatActivity { @Override protected void on ...