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 ...
随机推荐
- Qt中的多线程与线程池浅析+实例
1. Qt中的多线程与线程池 今天学习了Qt中的多线程和线程池,特写这篇博客来记录一下 2. 多线程 2.1 线程类 QThread Qt 中提供了一个线程类,通过这个类就可以创建子线程了,Qt 中一 ...
- 快速上手pandas(上)
pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation to ...
- 孟老板 Paging3 (一) 入门
BaseAdapter系列 ListAdapter系列 Paging3 (一) 入门 Paging3 (二) 结合 Room Paging3 (一) 入门 前言: 官方分页工具, 确实香. 但 ...
- 【单调栈】【前缀和】【二分查找】8.28题解-long
long 题目描述 AP神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M的矩形空地.空地中每个格子都有自己的海拔高度.AP想让他的宫殿的平均海拔在海平面之上(假设海平面的高度是0,平均数都会 ...
- 【NLP学习其二】什么是隐马尔可夫模型HMM?
概念 隐马尔可夫模型描述的是两个时序序列联合分布p(x,y)的概率模型,其中包含了两个序列: x序列外界可见(外界指的是观测者),称为观测序列(obsevation seuence) y序列外界不可见 ...
- 玩转STM32MP157- 使用fbtft驱动 lcd ili9341
之前使用了 fbtft 成功驱动了lcd st7735r,现在尝试下驱动 ili9341, 配置 跟之前用 fbtft 驱动 st7735r 一样,先用 make menuconfig 配置内核,添加 ...
- 『无为则无心』Python序列 — 17、Python字符串操作常用API
目录 1.字符串的查找 @1.find()方法 @2.index()方法 @3.rfind()和rindex()方法 @4.count()方法 2.字符串的修改 @1.replace()方法 @2.s ...
- Redis热点key优化
热门新闻事件或商品通常会给系统带来巨大的流量,对存储这类信息的Redis来说却是一个巨大的挑战.以Redis Cluster为例,它会造成整体流量的不均知,个别节点出现OPS过大的情况,极端情况下热点 ...
- Docker_CICD笔记
1 环境说明 1.1 机器配置 主机名称 IP地址 系统版本/内存/cpu核数/硬盘 安装软件 controlnode 172.16.1.70/24 centos7.4/4/2/60 docker.d ...
- 重新整理 .net core 实践篇————网关中的身份签名认证[三十七]
前言 简单整理一下网关中的jwt,jwt用于授权认证的,其实关于认证授权这块https://www.cnblogs.com/aoximin/p/12268520.html 这个链接的时候就已经写了,当 ...