目录
1、浅拷贝和深拷贝2、直接赋值3、for循环4、切片5、copy()方法(1)list.copy()方法(2)copy.copy()方法6、deepcopy()方法1、浅拷贝和深拷贝
浅拷贝复制指向某个对象的地址(指针),而不复制对象本身,新对象和原对象共享同一内存。
(相关资料图)
深拷贝会额外创建一个新的对象,新对象跟原对象并不共享内存,修改新对象不会影响到原对象。
赋值其实就是引用了原对象。两者指向同一内存,两个对象是联动的,无论哪个对象发生改变都会影响到另一个。
2、直接赋值
使用=来复制一个列表,实际上不仅复制了其中的内容,也复制了其内存地址,即引用了原列表。使用id()方法查看内存地址也是一样的。修改其中一个列表,也会直接更改另一个列表。
#!/usr/bin/env python # -*- coding:utf-8 -*- if __name__ == "__main__": a = ["a", ["b", "c", "d"], ["e", "f"]] # 直接复制,即引用列表 b = a print(a) print(b) # 通过id()查看内存地址,为一样的 print(id(a), id(b)) b[0] = "g" b[1][0] = "f" print(a) print(b)
输出:
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["b", "c", "d"], ["e", "f"]]
2030264836936 2030264836936
["g", ["f", "c", "d"], ["e", "f"]]
["g", ["f", "c", "d"], ["e", "f"]]
Process finished with exit code 0
3、for循环
使用for循环进行拷贝,仅第一层为深拷贝,对其它层依然是浅拷贝。
#!/usr/bin/env python # -*- coding:utf-8 -*- if __name__ == "__main__": a = ["a", ["b", "c", "d"], ["e", "f"]] b = [] # 使用for循环进行拷贝,仅第一层为深拷贝 for i in a: b.append(i) print(a) print(b) b[0] = "g" b[1][0] = "f" print(a) print(b)
输出:
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["f", "c", "d"], ["e", "f"]]
["g", ["f", "c", "d"], ["e", "f"]]
Process finished with exit code 0
4、切片
使用切片方法进行拷贝,也仅对第一层为深拷贝,对其它层依然是浅拷贝。
#!/usr/bin/env python # -*- coding:utf-8 -*- if __name__ == "__main__": a = ["a", ["b", "c", "d"], ["e", "f"]] # 使用切片进行拷贝,仅第一层为深拷贝 b = a[:] print(a) print(b) b[0] = "g" b[1][0] = "f" print(a) print(b)
输出:
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["f", "c", "d"], ["e", "f"]]
["g", ["f", "c", "d"], ["e", "f"]]
Process finished with exit code 0
5、copy()方法
(1)list.copy()方法
使用list.copy()方法进行拷贝,也仅对第一层为深拷贝,对其它层依然是浅拷贝。由于列表中嵌套的列表实际保存的是地址,依然指向同一个内存地址。
#!/usr/bin/env python # -*- coding:utf-8 -*- if __name__ == "__main__": a = ["a", ["b", "c", "d"], ["e", "f"]] # 使用list.copy()方法进行拷贝,仅第一层为深拷贝 b = a.copy() print(a) print(b) b[0] = "g" b[1][0] = "f" print(a) print(b)
输出:
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["f", "c", "d"], ["e", "f"]]
["g", ["f", "c", "d"], ["e", "f"]]
Process finished with exit code 0
(2)copy.copy()方法
使用copy.copy()方法进行拷贝,也仅对第一层为深拷贝,对其它层依然是浅拷贝。
#!/usr/bin/env python # -*- coding:utf-8 -*- import copy if __name__ == "__main__": a = ["a", ["b", "c", "d"], ["e", "f"]] # 使用copy.copy()方法进行拷贝,仅第一层为深拷贝 b = copy.copy(a) print(a) print(b) b[0] = "g" b[1][0] = "f" print(a) print(b)
输出:
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["f", "c", "d"], ["e", "f"]]
["g", ["f", "c", "d"], ["e", "f"]]
Process finished with exit code 0
6、deepcopy()方法
使用copy.deepcopy()方法进行拷贝,对所有层均为深拷贝,改变新列表并不会影响到原列表,推荐使用。
#!/usr/bin/env python # -*- coding:utf-8 -*- import copy if __name__ == "__main__": a = ["a", ["b", "c", "d"], ["e", "f"]] # 使用copy.deepcopy()方法进行拷贝,对所有层均为深拷贝 b = copy.deepcopy(a) print(a) print(b) b[0] = "g" b[1][0] = "f" print(a) print(b)
输出:
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["b", "c", "d"], ["e", "f"]]
["a", ["b", "c", "d"], ["e", "f"]]
["g", ["f", "c", "d"], ["e", "f"]]
Process finished with exit code 0
到此这篇关于python中list列表复制的几种方法(赋值、切片、copy(),deepcopy())的文章就介绍到这了,更多相关python list列表复制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
X 关闭
X 关闭
- 1亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 2现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 3如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 4AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 5转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 6充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 7好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 8名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?
- 9亚马逊云科技成立量子网络中心致力解决量子计算领域的挑战
- 10京东绿色建材线上平台上线 新增用户70%来自下沉市场