浅拷贝
# 第一种
In [1]: x = [1,2,3]
In [2]: y = x.copy()
In [3]: x[1] = 1
In [4]: x
Out[4]: [1, 1, 3]
In [5]: y
Out[5]: [1, 2, 3]
# 第二种
In [6]: x = [1,2,3]
In [7]: y = x[:]
In [8]: x[1] = 1
In [9]: x
Out[9]: [1, 1, 3]
In [10]: y
Out[10]: [1, 2, 3]
# 第三种
In [11]: x = [[1,2,3],[4,5,6],[7,8,9]]
In [12]: y = x.copy()
In [13]: x[1][1] = 0
In [14]: x
Out[14]: [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
In [15]: y
Out[15]: [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
# 第四种
In [16]: import copy
In [17]: x = [[1,2,3],[4,5,6],[7,8,9]]
In [18]: y = copy.copy(x)
In [19]: x[1][1] = 0
In [20]: x
Out[20]: [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
In [21]: y
Out[21]: [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
深拷贝
In [22]: x = [[1,2,3],[4,5,6],[7,8,9]]
In [23]: y = copy.deepcopy(x)
In [24]: x[1][1] = 0
In [25]: x
Out[25]: [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
In [26]: y
Out[26]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
标签:25,26,20,Python,拷贝,copy,Out
From: https://www.cnblogs.com/2ich4n/p/18104869