Python - 浅拷贝的四种实现方式
浅拷贝详解
https://www.cnblogs.com/poloyy/p/15084277.html
方式一:使用切片 [:]
列表
# 浅拷贝 [:]
old_list = [1, 2, [3, 4]]
new_list = old_list[:] old_list.append(5)
old_list[2][0] += 97 print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2])) # 输出结果
Old list: [1, 2, [100, 4], 5] old list id: 4537660608 old list[0] id: 4537659840
new list: [1, 2, [100, 4]] new list id: 4537711424 new list[0] id: 4537659840
方式二:使用工厂函数
工厂函数简介
- 工厂函数看上去像函数,但实际是一个类
- 调用时,生成该数据类型类型的一个实例
可变对象的工厂函数
- list()
- set()
- dict()
列表
# 浅拷贝 工厂函数
old_list = [1, 2, [3, 4]]
new_list = list(old_list) old_list.append(5)
old_list[2][0] += 97 print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))
集合
# 浅拷贝 工厂函数-集合
old_set = {1, 2, 3}
new_set = set(old_set) old_set.add(4) print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set)) # 输出结果
Old set: {1, 2, 3, 4} old set id: 4484723648
new set: {1, 2, 3} new set id: 4484723872
字典
# 浅拷贝 工厂函数-字典
old_dict = {"name": "小菠萝"}
new_dict = dict(old_dict) old_dict["second"] = "测试笔记" print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict)) # 输出结果
Old dict: {'name': '小菠萝', 'second': '测试笔记'} old dict id: 4514161536
new dict: {'name': '小菠萝'} new dict id: 4515690304
方式三:使用数据类型自带的 copy 方法
列表
# 浅拷贝 自带的copy方法-列表
old_list = [1, 2, [3, 4]]
new_list = old_list.copy() old_list.append(5)
old_list[2][0] += 97 print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2])) # 输出结果
Old list: [1, 2, [100, 4], 5] old list id: 4309832000 old list[0] id: 4310372992
new list: [1, 2, [100, 4]] new list id: 4309735296 new list[0] id: 4310372992
集合
# 浅拷贝 自带的copy方法-集合
old_set = {1, 2, 3}
new_set = old_set.copy() old_set.add(4) print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set)) # 输出结果
Old set: {1, 2, 3, 4} old set id: 4309931392
new set: {1, 2, 3} new set id: 4309930944
字典
# 浅拷贝 自带的copy方法-字典
old_dict = {"name": "小菠萝"}
new_dict = old_dict.copy() old_dict["second"] = "测试笔记" print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict)) # 输出结果
Old dict: {'name': '小菠萝', 'second': '测试笔记'} old dict id: 4308452288
new dict: {'name': '小菠萝'} new dict id: 4308452224
源码
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of the list. """
pass
已经写的很清楚,这是浅拷贝
方式四:使用 copy 模块的 copy 方法
列表
# 浅拷贝 copy模块的copy方法-列表
from copy import copy old_list = [1, 2, [3, 4]]
new_list = copy(old_list) old_list.append(5)
old_list[2][0] += 97 print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2])) # 输出结果
Old list: [1, 2, [100, 4], 5] old list id: 4381013184 old list[0] id: 4381159936
new list: [1, 2, [100, 4]] new list id: 4381012800 new list[0] id: 4381159936
集合
# 浅拷贝 copy模块的copy方法-集合
from copy import copy old_set = {1, 2, 3}
new_set = copy(old_set) old_set.add(4) print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set)) # 输出结果
Old set: {1, 2, 3, 4} old set id: 4381115552
new set: {1, 2, 3} new set id: 4381115776
字典
# 浅拷贝 copy模块的copy方法-字典
from copy import copy old_dict = {"name": "小菠萝"}
new_dict = copy(old_dict) old_dict["second"] = "测试笔记" print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict)) # 输出结果
Old dict: {'name': '小菠萝', 'second': '测试笔记'} old dict id: 4381159680
new dict: {'name': '小菠萝'} new dict id: 4379632576
Python - 浅拷贝的四种实现方式的更多相关文章
- python 单例模式的四种创建方式
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- Python文件的四种读写方式——r a w r+
# 文件的基本操作,但是一般不这么使用,因为经常会忘记关闭 password=open("abc.txt",mode="r",encoding="UT ...
- js的数据类型、函数、流程控制及变量的四种声明方式
运算符 基本运算符 加 + 减 - 乘 * 除 / 取余 % 自增 ++ eg: 1++ 或 ++1 自减 -- eg: 1-- 或 --1 注:++或--写在前面表示优先级最高,先进行自增或者自减 ...
- Android开发之基本控件和详解四种布局方式
Android中的控件的使用方式和iOS中控件的使用方式基本相同,都是事件驱动.给控件添加事件也有接口回调和委托代理的方式.今天这篇博客就总结一下Android中常用的基本控件以及布局方式.说到布局方 ...
- lua中for循环的四种遍历方式
lua中for的四种遍历方式区别 table.maxn 取最大的整数key #table 从1开始的顺序整数最大值,如1,2,3,6 #table == 3 key,value pairs 取每一 ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
- HttpwebClient的四种请求方式
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷. 本文旨在发布代码,供自己参考,也供大家参考,谢谢. 正题: Ht ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...
随机推荐
- Paxos 图解 (秒懂)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- ubuntu初识
简单的Linux系统理解 Ubuntu ctrl+alt 可以令虚拟机释放鼠标,回到主系统中. 如果ubuntu有问题,如何重装? -------直接删除目录.虚拟机内的操作不会影响到宿主机. 1.挂 ...
- 计算机网络的参考模型与5G协议
一.分层思想 二.OSI七层参考模型 三.FPC/IP五层模型 四.数据的封装过程与PDU(协议数据单元) 五.数据的解封装过程 六.各层间通信与设备与层的对应关系 七.总结 一.分层思想 将复杂的 ...
- SSH远程登录相关教程
命令概述 命令 英文 ssh 用户名@ip secure shell scp 用户名@ip:文件名或路径 用户名@ip:文件名或路径 secure copy 在 Linux 中 SSH 是 非常常用 ...
- UnityPlayerActivity删除后的后果
刚踩完这个坑,来说一下吧! 原因: 我因为前阵子学习了一下Unity Android交互,在这个过程中,我创建了类库,在类库里因为要用UnityPlayerActivity.java类所以便把Unit ...
- 04 jumpserver资产管理
4.资产管理: (1)管理用户: 管理用户是资产(被控服务器)上的 root,或拥有 NOPASSWD: ALL sudo 权限的用户, JumpServer 使用该用户来 `推送系统用户`.`获取资 ...
- ECS实例中的应用偶尔出现丢包现象并且内核日志(dmesg)存在“kernel: nf_conntrack: table full, dropping packet”的报错信息
问题描述 连接ECS实例中的应用时偶尔出现丢包现象.经排查,ECS实例的外围网络正常,但内核日志(dmesg)中存在"kernel: nf_conntrack: table full, dr ...
- salesforce零基础学习(一百零四)Salesforce Optimizer
本篇参考: https://admin.salesforce.com/blog/2017/analyzing-org-salesforce-optimizer-webinar-recap 假设你在做一 ...
- SpringCloud 微服务最佳开发实践
Maven规范 所有项目必须要有一个统一的parent模块 所有微服务工程都依赖这个parent,parent用于管理依赖版本,maven仓库,jar版本的统一升级维护 在parent下层可以有 co ...
- Postgresql常见操作命令
安装Postgresql 请查看我的另一篇博文: 博文连接:https://www.cnblogs.com/cndevops/p/14962745.html 连接Postgresql数据库 服务端连接 ...