Python中浅拷贝的四种实现方法小结

 更新时间:2021年11月04日 17:14:20   作者:qdPython  
本文主要介绍了Python中浅拷贝的四种实现方法小结,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

方式一:使用切片 [:]

列表

# 浅拷贝 [:]
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"] = "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: 4514161536
new 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] += 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

集合

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

字典

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: 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 方法

列表

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

集合

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

字典

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))


# 输出结果
Old dict: {'name': '小明', 'second': 'Python'} old dict id: 4381159680
new dict: {'name': '小明'} new dict id: 4379632576

到此这篇关于Python中浅拷贝的四种实现方法小结的文章就介绍到这了,更多相关Python 浅拷贝内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Python 实现打印单词的菱形字符图案

    Python 实现打印单词的菱形字符图案

    这篇文章主要介绍了Python 实现打印单词的菱形字符图案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • python利用 pytesseract快速识别提取图片中的文字((图片识别)

    python利用 pytesseract快速识别提取图片中的文字((图片识别)

    本文介绍了tesseract的python调用,也就是pytesseract库,其中还有一些其他的内容并没有涉及,仅涉及到了图片提取文字,如果你对其感兴趣,可以深入探索一下,也希望能和我探讨一下
    2022-11-11
  • Python实现向PPT中插入表格与图片的方法详解

    Python实现向PPT中插入表格与图片的方法详解

    这篇文章将带大家学习一下如何在PPT中插入表格与图片以及在表格中插入内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-05-05
  • 在Tensorflow中实现leakyRelu操作详解(高效)

    在Tensorflow中实现leakyRelu操作详解(高效)

    这篇文章主要介绍了在Tensorflow中实现leakyRelu操作详解(高效),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • 基于Python+QT的gui程序开发实现

    基于Python+QT的gui程序开发实现

    这篇文章主要介绍了基于Python+QT的gui程序开发实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 解读Python中字典的key都可以是什么

    解读Python中字典的key都可以是什么

    这篇文章主要介绍了解读Python中字典的key都可以是什么,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Django xadmin安装及使用详解

    Django xadmin安装及使用详解

    这篇文章主要介绍了Django xadmin安装及使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • python实现zencart产品数据导入到magento(python导入数据)

    python实现zencart产品数据导入到magento(python导入数据)

    这篇文章主要介绍了python实现zencart产品数据导入到magento(python导入数据),需要的朋友可以参考下
    2014-04-04
  • python中requests库+xpath+lxml简单使用

    python中requests库+xpath+lxml简单使用

    这篇文章主要介绍了python中requests库+xpath+lxml简单使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 利用python程序生成word和PDF文档的方法

    利用python程序生成word和PDF文档的方法

    这篇文章主要给大家介绍了利用python程序生成word和PDF文档的方法,文中给出了详细的介绍和示例代码,相信对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。
    2017-02-02

最新评论