翼度科技»论坛 编程开发 python 查看内容

四种实现方式实现Python浅拷贝

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
方式一:使用切片 [:]

列表
  1. # 浅拷贝 [:]
  2. old_list = [1, 2, [3, 4]]
  3. new_list = old_list[:]
  4. old_list.append(5)
  5. old_list[2][0] += 97
  6. print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
  7. print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))
  8. # 输出结果
  9. Old list: [1, 2, [100, 4], 5] old list id: 4537660608  old list[0] id: 4537659840
  10. new list: [1, 2, [100, 4]] new list id: 4537711424  new list[0] id: 4537659840
复制代码
方式二:使用工厂函数

工厂函数简介

  • 工厂函数看上去像函数,但实际是一个类
  • 调用时,生成该数据类型类型的一个实例
可变对象的工厂函数

  • list()
  • set()
  • dict()
列表
  1. # 浅拷贝 工厂函数
  2. old_list = [1, 2, [3, 4]]
  3. new_list = list(old_list)
  4. old_list.append(5)
  5. old_list[2][0] += 97
  6. print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
  7. print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))
复制代码
集合
  1. # 浅拷贝 工厂函数-集合
  2. old_set = {1, 2, 3}
  3. new_set = set(old_set)
  4. old_set.add(4)
  5. print("Old set:", old_set, "old set id:", id(old_set))
  6. print("new set:", new_set, "new set id:", id(new_set))
  7. # 输出结果
  8. Old set: {1, 2, 3, 4} old set id: 4484723648
  9. new set: {1, 2, 3} new set id: 4484723872
复制代码
字典
  1. # 浅拷贝 工厂函数-字典
  2. old_dict = {"name": "小菠萝"}
  3. new_dict = dict(old_dict)
  4. old_dict["second"] = "测试笔记"
  5. print("Old dict:", old_dict, "old dict id:", id(old_dict))
  6. print("new dict:", new_dict, "new dict id:", id(new_dict))
  7. # 输出结果
  8. Old dict: {'name': '小菠萝', 'second': '测试笔记'} old dict id: 4514161536
  9. new dict: {'name': '小菠萝'} new dict id: 4515690304
复制代码
方式三:使用数据类型自带的 copy 方法

列表
  1. # 浅拷贝 自带的copy方法-列表
  2. old_list = [1, 2, [3, 4]]
  3. new_list = old_list.copy()
  4. old_list.append(5)
  5. old_list[2][0] += 97
  6. print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
  7. print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))
  8. # 输出结果
  9. Old list: [1, 2, [100, 4], 5] old list id: 4309832000  old list[0] id: 4310372992
  10. new list: [1, 2, [100, 4]] new list id: 4309735296  new list[0] id: 4310372992
复制代码
集合
  1. # 浅拷贝 自带的copy方法-集合
  2. old_set = {1, 2, 3}
  3. new_set = old_set.copy()
  4. old_set.add(4)
  5. print("Old set:", old_set, "old set id:", id(old_set))
  6. print("new set:", new_set, "new set id:", id(new_set))
  7. # 输出结果
  8. Old set: {1, 2, 3, 4} old set id: 4309931392
  9. new set: {1, 2, 3} new set id: 4309930944
复制代码
字典
  1. # 浅拷贝 自带的copy方法-字典
  2. old_dict = {"name": "小明"}
  3. new_dict = old_dict.copy()
  4. old_dict["second"] = "python"
  5. print("Old dict:", old_dict, "old dict id:", id(old_dict))
  6. print("new dict:", new_dict, "new dict id:", id(new_dict))
  7. # 输出结果
  8. Old dict: {'name': '小明', 'second': 'python'} old dict id: 1611676464896
  9. new dict: {'name': '小明'} new dict id: 1611676464960
复制代码
源码
  1. def copy(self, *args, **kwargs): # real signature unknown
  2.     """ Return a shallow copy of the list. """
  3.     pass
复制代码
已经写的很清楚,这是浅拷贝
方式四:使用 copy 模块的 copy 方法

列表
  1. # 浅拷贝 copy模块的copy方法-列表
  2. from copy import copy
  3. old_list = [1, 2, [3, 4]]
  4. new_list = copy(old_list)
  5. old_list.append(5)
  6. old_list[2][0] += 97
  7. print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
  8. print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))
  9. # 输出结果
  10. Old list: [1, 2, [100, 4], 5] old list id: 4381013184  old list[0] id: 4381159936
  11. new list: [1, 2, [100, 4]] new list id: 4381012800  new list[0] id: 4381159936
复制代码
集合
  1. # 浅拷贝 copy模块的copy方法-集合
  2. from copy import copy
  3. old_set = {1, 2, 3}
  4. new_set = copy(old_set)
  5. old_set.add(4)
  6. print("Old set:", old_set, "old set id:", id(old_set))
  7. print("new set:", new_set, "new set id:", id(new_set))
  8. # 输出结果
  9. Old set: {1, 2, 3, 4} old set id: 4381115552
  10. new set: {1, 2, 3} new set id: 4381115776
复制代码
字典
  1. # 浅拷贝 copy模块的copy方法-字典
  2. from copy import copy
  3. old_dict = {"name": "小明"}
  4. new_dict = copy(old_dict)
  5. old_dict["second"] = "python"
  6. print("Old dict:", old_dict, "old dict id:", id(old_dict))
  7. print("new dict:", new_dict, "new dict id:", id(new_dict))
  8. # Python小白学习交流群:153708845
  9. # 输出结果
  10. Old dict: {'name': '小明', 'second': 'python'} old dict id: 3220382234368
  11. new dict: {'name': '小明'} new dict id: 3220382234432
复制代码
来源:https://www.cnblogs.com/djdjdj123/p/17782917.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具