Python中浅拷贝的实现方法有哪些
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要介绍"Python中浅拷贝的实现方法有哪些",在日常操作中,相信很多人在Python中浅拷贝的实现方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"P
千家信息网最后更新 2025年11月08日Python中浅拷贝的实现方法有哪些方式一:使用切片 [:]
方式三:使用数据类型自带的 copy 方法
方式四:使用 copy 模块的 copy 方法
这篇文章主要介绍"Python中浅拷贝的实现方法有哪些",在日常操作中,相信很多人在Python中浅拷贝的实现方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Python中浅拷贝的实现方法有哪些"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
方式一:使用切片 [:]
列表
# 浅拷贝 [:]old_list = [1, 2, [3, 4]]new_list = old_list[:]old_list.append(5)old_list[2][0] += 97print("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: 4537659840new 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] += 97print("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: 4484723648new set: {1, 2, 3} new set id: 4484723872字典
old_dict = {"name": "小明"}new_dict = dict(old_dict)old_dict["second"] = "Python"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': 'Python'} old dict id: 4514161536new dict: {'name': '小明'} new dict id: 4515690304方式三:使用数据类型自带的 copy 方法
列表
old_list = [1, 2, [3, 4]]new_list = old_list.copy()old_list.append(5)old_list[2][0] += 97print("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: 4310372992new list: [1, 2, [100, 4]] new list id: 4309735296 new list[0] id: 4310372992集合
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: 4309931392new set: {1, 2, 3} new set id: 4309930944字典
old_dict = {"name": "小明"}new_dict = old_dict.copy()old_dict["second"] = "Python"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': 'Python'} old dict id: 4308452288new dict: {'name': '小明'} new dict id: 4308452224源码
def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of the list. """ pass
已经写的很清楚,这是浅拷贝
方式四:使用 copy 模块的 copy 方法
列表
from copy import copyold_list = [1, 2, [3, 4]]new_list = copy(old_list)old_list.append(5)old_list[2][0] += 97print("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: 4381159936new list: [1, 2, [100, 4]] new list id: 4381012800 new list[0] id: 4381159936集合
from copy import copyold_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: 4381115552new set: {1, 2, 3} new set id: 4381115776字典
from copy import copyold_dict = {"name": "小明"}new_dict = copy(old_dict)old_dict["second"] = "Python"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': 'Python'} old dict id: 4381159680new dict: {'name': '小明'} new dict id: 4379632576到此,关于"Python中浅拷贝的实现方法有哪些"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
结果
小明
输出
方法
拷贝
函数
工厂
方式
学习
字典
类型
数据
更多
帮助
实用
清楚
接下来
实例
实际
对象
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全的技术因素是哪4种
网络安全手抄报 视频
史上网络安全手抄报
价格 服务器管理
软件开发人员技术晋升通道
做软件开发压力大
科技互联网下的创业思维
深圳市网络技术
南阳网络安全专业的公司
人工智能与网络安全的构想
网络安全加固记录
浪潮服务器黑龙江经销商
服务器开机后只有c盘
一键搭建云服务器脚本
steam哪个服务器不要钱
搭建流媒体服务器 rtsp
佛山服务器机房
魔兽世界服务器转移问题
云需要服务器吗
哈尔滨海上飞互联网科技有限公司
java服务器
安捷鸽钟服务器号码各地是否一致
mysql 数据库快照
数据库操作记录保存多久
骏越app服务器连接不上
网站后台数据库管理
四川软件开发制作
搭建流媒体服务器 rtsp
海外服务器的软件下载慢吗
阿里云空间服务器配置