首頁 > 軟體

python中list列表複製的幾種方法(賦值、切片、copy(),deepcopy())

2022-08-19 22:01:34

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列表複製內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com