和天斗 发表于 2023-10-24 01:58:49

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

方式一:使用切片 [:]

列表
# 浅拷贝 [:]
old_list = ]
new_list = old_list[:]

old_list.append(5)
old_list += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list id:", id(old_list))
print("new list:", new_list, "new list id:", id(new_list), " new list id:", id(new_list))


# 输出结果
Old list: , 5] old list id: 4537660608old list id: 4537659840
new list: ] new list id: 4537711424new list id: 4537659840方式二:使用工厂函数

工厂函数简介

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

[*]list()
[*]set()
[*]dict()
列表
# 浅拷贝 工厂函数
old_list = ]
new_list = list(old_list)

old_list.append(5)
old_list += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list id:", id(old_list))
print("new list:", new_list, "new list id:", id(new_list), " new list id:", id(new_list))集合
# 浅拷贝 工厂函数-集合
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 = ]
new_list = old_list.copy()

old_list.append(5)
old_list += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list id:", id(old_list))
print("new list:", new_list, "new list id:", id(new_list), " new list id:", id(new_list))


# 输出结果
Old list: , 5] old list id: 4309832000old list id: 4310372992
new list: ] new list id: 4309735296new list 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"] = "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: 1611676464896
new dict: {'name': '小明'} new dict id: 1611676464960源码
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 = ]
new_list = copy(old_list)

old_list.append(5)
old_list += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list id:", id(old_list))
print("new list:", new_list, "new list id:", id(new_list), " new list id:", id(new_list))


# 输出结果
Old list: , 5] old list id: 4381013184old list id: 4381159936
new list: ] new list id: 4381012800new list 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"] = "python"

print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict))


# Python小白学习交流群:153708845
# 输出结果
Old dict: {'name': '小明', 'second': 'python'} old dict id: 3220382234368
new dict: {'name': '小明'} new dict id: 3220382234432
来源:https://www.cnblogs.com/djdjdj123/p/17782917.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 四种实现方式实现Python浅拷贝