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 ...
随机推荐
- 懒人 IDEA 插件推荐:EasyCode 一键帮你生成所需代码
Easycode是idea的一个插件,可以直接对数据的表生成entity,controller,service,dao,mapper,无需任何编码,简单而强大. 1.安装(EasyCode) 我这里的 ...
- Python常用数据结构(列表)
Python中常用的数据结构有序列(如列表,元组,字符串),映射(如字典)以及集合(set),是主要的三类容器 内容 序列的基本概念 列表的概念和用法 元组的概念和用法 字典的概念和用法 各类型之间的 ...
- Linux中date的用法
一.命令格式:date [参数]... [+格式]二.命令功能:date 可以用来显示或设定系统的日期与时间.三.命令格式:%H 小时(以00-23来表示). %I 小时(以01-12来表示). %K ...
- 17、linux root用户密码找回
17.1.救援模式: 光盘模式启动(第一启动项) 删除/mnt/sysimage/etc/passwd root的密码,halt重启. 改为硬盘启动模式,无密码进入root,为root新建密码 17. ...
- IDEA详细配置+优秀插件
目录 IDEA破解 Settings配置 配置 settings 字体 关闭IDEA更新 设置IDEA打开为项目选择界面 自动导入包配置 显示方法的分割线 滚轮设置字体大小 智能提示忽略大小写 Tab ...
- 闲聊,Python中的turtle
写在前面 其实我也不知道为什么我会写这个,本文涉及信号与传递,Python 正题 近期看到一个3年前的视频,1000个圆一笔画出一个Miku 在观看完源码了以后,我发现这是这调用的是基本的goto,用 ...
- Kafka常用命令及详细介绍
目录 常用操作 Sentry kafka 清理 Kafka 术语 Kafka 主题剖析 Kafka 生产者 kafka 消费者和消费组 一致性和可用性 写入处理 失败处理 Kafka 客户端一致性 文 ...
- Postgresql:postgres命令行导入sql文件
sql文件导入 psql -d jdbc -h localhost -p 5432 -U postgres -f /home/sql/test.sql #-d 数据库名称 #-h ip地址 #-p 端 ...
- JAVA WEB 用servlet实现分页,思路比较清晰和简单。
JAVA WEB 用servlet实现分页,思路比较清晰和简单.借鉴了其他大佬的思路.特别感谢. 是我第一次发表博客,如果有什么错误,欢迎大家指出!,谢谢 一.思路分析 前台一定是有类似这种的界面 点 ...
- SQL 查询并不是从 SELECT 开始的
原文地址:SQL queries don't start with SELECT 原文作者:Julia Evans(已授权) 译者 & 校正:HelloGitHub-小熊熊 & 卤蛋 ...