亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中l(wèi)ist列表復(fù)制的幾種方法(賦值、切片、copy(),deepcopy())

 更新時(shí)間:2022年08月19日 16:08:20   作者:程序遇上智能星空  
本文主要介紹了python中l(wèi)ist列表復(fù)制的幾種方法(賦值、切片、copy(),deepcopy()),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1、淺拷貝和深拷貝

淺拷貝復(fù)制指向某個(gè)對(duì)象的地址(指針),而不復(fù)制對(duì)象本身,新對(duì)象和原對(duì)象共享同一內(nèi)存。

深拷貝會(huì)額外創(chuàng)建一個(gè)新的對(duì)象,新對(duì)象跟原對(duì)象并不共享內(nèi)存,修改新對(duì)象不會(huì)影響到原對(duì)象。

賦值其實(shí)就是引用了原對(duì)象。兩者指向同一內(nèi)存,兩個(gè)對(duì)象是聯(lián)動(dòng)的,無(wú)論哪個(gè)對(duì)象發(fā)生改變都會(huì)影響到另一個(gè)。

2、直接賦值

使用=來復(fù)制一個(gè)列表,實(shí)際上不僅復(fù)制了其中的內(nèi)容,也復(fù)制了其內(nèi)存地址,即引用了原列表。使用id()方法查看內(nèi)存地址也是一樣的。修改其中一個(gè)列表,也會(huì)直接更改另一個(gè)列表。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
if __name__ == '__main__':
    a = ["a", ["b", "c", "d"], ["e", "f"]]
    # 直接復(fù)制,即引用列表
    b = a
    print(a)
    print(b)
    # 通過id()查看內(nèi)存地址,為一樣的
    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循環(huán)

使用for循環(huán)進(jìn)行拷貝,僅第一層為深拷貝,對(duì)其它層依然是淺拷貝。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
if __name__ == '__main__':
    a = ["a", ["b", "c", "d"], ["e", "f"]]
    b = []
    # 使用for循環(huán)進(jìn)行拷貝,僅第一層為深拷貝
    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、切片

使用切片方法進(jìn)行拷貝,也僅對(duì)第一層為深拷貝,對(duì)其它層依然是淺拷貝。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
if __name__ == '__main__':
    a = ["a", ["b", "c", "d"], ["e", "f"]]
    # 使用切片進(jìn)行拷貝,僅第一層為深拷貝
    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()方法進(jìn)行拷貝,也僅對(duì)第一層為深拷貝,對(duì)其它層依然是淺拷貝。由于列表中嵌套的列表實(shí)際保存的是地址,依然指向同一個(gè)內(nèi)存地址。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
if __name__ == '__main__':
    a = ["a", ["b", "c", "d"], ["e", "f"]]
    # 使用list.copy()方法進(jìn)行拷貝,僅第一層為深拷貝
    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()方法進(jìn)行拷貝,也僅對(duì)第一層為深拷貝,對(duì)其它層依然是淺拷貝。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import copy
 
if __name__ == '__main__':
    a = ["a", ["b", "c", "d"], ["e", "f"]]
    # 使用copy.copy()方法進(jìn)行拷貝,僅第一層為深拷貝
    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()方法進(jìn)行拷貝,對(duì)所有層均為深拷貝,改變新列表并不會(huì)影響到原列表,推薦使用。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import copy
 
if __name__ == '__main__':
    a = ["a", ["b", "c", "d"], ["e", "f"]]
    # 使用copy.deepcopy()方法進(jìn)行拷貝,對(duì)所有層均為深拷貝
    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

到此這篇關(guān)于python中l(wèi)ist列表復(fù)制的幾種方法(賦值、切片、copy(),deepcopy())的文章就介紹到這了,更多相關(guān)python list列表復(fù)制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論