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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
2020-08-01 性能测试必备知识(8)- 深入理解“进程状态”