Python中List的复制(直接复制、浅拷贝、深拷贝)

1,512次阅读
没有评论

直接赋值:
如果用 = 直接赋值,是非拷贝方法。

这两个列表是等价的,修改其中任何一个列表都会影响到另一个列表。

old = [1,[1,2,3],3]
new = []
for i in range(len(old)):
new.append(old[i])

new[0] = 3
new[1][0] = 3

”’

Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[3, [3, 2, 3], 3]

[3, [3, 2, 3], 3]

”’

浅拷贝:
1.copy()方法
对于List来说,其第一层,是实现了深拷贝,但对于其内嵌套的List,仍然是浅拷贝。

因为嵌套的List保存的是地址,复制过去的时候是把地址复制过去了,嵌套的List在内存中指向的还是同一个。

old = [1,[1,2,3],3]
new = old.copy()

new[0] = 3
new[1][0] =3

”’

Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]

[3, [3, 2, 3], 3]

”’

2.使用列表生成式
使用列表生成式产生新列表也是一个浅拷贝方法,只对第一层实现深拷贝。

old = [1,[1,2,3],3]
new = [i for i in old]

new[0] = 3
new[1][0] = 3

”’

Before
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After
[1, [3, 2, 3], 3]

[3, [3, 2, 3], 3]

”’

3.for循环遍历
通过for循环遍历,将元素一个个添加到新列表中。这也是一个浅拷贝方法,只对第一层实现深拷贝。

old = [1,[1,2,3],3]
new = []
for i in range(len(old)):
new.append(old[i])

new[0] = 3
new[1][0] = 3

”’

Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]

[3, [3, 2, 3], 3]

”’

4.使用切片
通过使用 [ : ] 切片,可以浅拷贝整个列表,同样的,只对第一层实现深拷贝。

old = [1,[1,2,3],3]
new = old[:]

new[0] = 3
new[1][0] = 3

”’

Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]

[3, [3, 2, 3], 3]

”’

深拷贝:
如果用deepcopy()方法,则无论多少层,无论怎样的形式,得到的新列表都是和原来无关的,这是最安全最清爽最有效的方法。

需要import copy

import copy
old = [1,[1,2,3],3]
new = copy.deepcopy(old)

new[0] = 3
new[1][0] = 3

”’

Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [1, 2, 3], 3]

[3, [3, 2, 3], 3]

”’

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2024 年 3 月
 123
45678910
11121314151617
18192021222324
25262728293031
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
手把手教你用AI 10分钟生成一个APP!零基础也能搞定

手把手教你用AI 10分钟生成一个APP!零基础也能搞定

今日,我将向大家展示DeepSeek的全新玩法——从零开始,利用AI创建一个完整的应用程序。借助DeepSee...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
手把手教你用AI 10分钟生成一个APP!零基础也能搞定

手把手教你用AI 10分钟生成一个APP!零基础也能搞定

今日,我将向大家展示DeepSeek的全新玩法——从零开始,利用AI创建一个完整的应用程序。借助DeepSee...