全球讯息:python中list列表复制的几种方法(赋值、切片、copy(),deepcopy())
来源:脚本之家    时间:2022-08-19 19:49:59
目录
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 关闭