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的更多相关文章

  1. python 集合和深浅copy

    #1数据类型的补充#2.集合set#3.深浅copy 补充:str --> bytes s.encode('gbk')bytes --> str s.decode('gbk') 1.数据类 ...

  2. Python基础—set、copy(Day7)

    一.数据类型补充 1.str:.isspace()字符串是空格或至少是一个空格. s='alex' s1=' ' ret=s1.isspace() print(ret)执行结果:True  2.tul ...

  3. python基础05--深浅copy, set,bytes

    1.1 深浅 copy 1. =  赋值操作, lis1=[1,2,3]  list2 = list1  list1.append(4)  则list1,list2都变 赋值都指向同一个地址,改变一个 ...

  4. python基础(6)-深浅拷贝

    赋值 字符串和数字 # id()函数可以获取变量在内存中的地址标识 num1 = 2; num2 = 2; print(id(num1)) # result:8791124202560 print(i ...

  5. python 基础之深浅拷贝

    深浅拷贝 s=[[1,2],'fgfgf','cx'] s3=s.copy() print(s) print(s3) 测试 D:\python\python.exe D:/untitled/dir/f ...

  6. Python基础:深浅拷贝

    对于数字.字符串深浅拷贝: import copy num = 0 copy_num = copy.copy(num) print("These are normal copy") ...

  7. python学习日记(深浅copy)

    赋值 #赋值,指向同一内存地址 l1 = [1,2,3,4,5] l2 = l1 print(l1,l2) print(id(l1),id(l2)) 浅copy #浅copy,第一层互相独立,创建了新 ...

  8. python入门之深浅copy

    a1=["a","b","c","aa"] b1=a1 a1[0]=" print(a1,b1) 此时结果为: ...

  9. python基础七之copy

    浅拷贝 没有嵌套,则copy后完全不同,有嵌套,则copy后本体不同,嵌套相同. l1 = [1, 2, [4, 5, 6], 3] l2 = l1.copy() print(l1 is l2) # ...

随机推荐

  1. 怎样理解JS的预解析机制

    JS的预解析包括两部分: 1. 变量提升 2. 函数声明 对于变量提升, 可以看下下面这块代码 console.log(name); // undefined var name = "Lil ...

  2. HTTP协议探究(三):HTTPS

    一 复习与目标 1 复习 代理:转发通信数据(一般协议不变,作为中间人,可对报文进行过滤修改) 网关:转发通信数据(协议改变,作为资源拥有者) 隧道:转发通信数据(协议不变,作为管道,不对报文进行过滤 ...

  3. 高性能网站建设之 MS Sql Server数据库分区

    什么是数据库分区?数据库分区是一种对表的横向分割,Sql server 2005企业版和之后的Sql server版本才提供这种技术,这种对表的横向分割不同于2000中的表分割,它对访问用户是透明的, ...

  4. async/await 的引用

    static async void Start() { string s = "ass"; Console.WriteLine(getMemory(s)+"Hello W ...

  5. [转载]目标检测-Selective Search

    [转载]目标检测-Selective Search 转载纯粹是因为这篇写的很好,mark一下: https://zhuanlan.zhihu.com/p/27467369

  6. 在Pytorch上使用稀疏矩阵

    在Pytorch上使用稀疏矩阵 最近在写一个NLP的小项目,用到了Pytorch做神经网络模型.但是众所周知NLP的一个特点就是特征矩阵是稀疏矩阵,当时处理稀疏矩阵用的是scipy.sparse,现在 ...

  7. php实现多进程、多线程

    孤儿进程:一个父进程退出,而它的一个或多个子进程还在运行,那么那些子进程将成为孤儿进程.孤儿进程将被init进程(进程号为1)所收养,并由init进程对它们完成状态收集工作. 僵尸进程:一个进程使用f ...

  8. Spring Cloud(六)服务网关 zuul 快速入门

    服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...

  9. python学习笔记:安装boost python库以及使用boost.python库封装

    学习是一个累积的过程.在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结. 前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C+ ...

  10. sklearn--模型的评价

    sklearn.metrics 1.MSE(均方误差)和RMSE(均方根误差),以及score() lr.score(test_x,test_y)#越接近1越好,负的很差 from sklearn.m ...