|
目录
- 方式一:使用切片 [:]
- 方式二:使用工厂函数
- 方式三:使用数据类型自带的 copy 方法
- 方式四:使用 copy 模块的 copy 方法
方式一:使用切片 [:]
列表- # 浅拷贝 [:]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
复制代码 方式二:使用工厂函数
工厂函数简介
- 工厂函数看上去像函数,但实际是一个类
- 调用时,生成该数据类型类型的一个实例
可变对象的工厂函数
列表- 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中浅拷贝的四种实现方法小结的文章就介绍到这了,更多相关Python 浅拷贝内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:http://www.jb51.net/article/227354.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|