欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中淺拷貝的四種實(shí)現(xiàn)方法小結(jié)

 更新時(shí)間:2021年11月04日 17:14:20   作者:qdPython  
本文主要介紹了Python中淺拷貝的四種實(shí)現(xiàn)方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

方式一:使用切片 [:]

列表

# 淺拷貝 [:]
old_list = [1, 2, [3, 4]]
new_list = old_list[:]

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))


# 輸出結(jié)果
Old list: [1, 2, [100, 4], 5] old list id: 4537660608  old list[0] id: 4537659840
new list: [1, 2, [100, 4]] new list id: 4537711424  new list[0] id: 4537659840

方式二:使用工廠函數(shù)

工廠函數(shù)簡(jiǎn)介

  • 工廠函數(shù)看上去像函數(shù),但實(shí)際是一個(gè)類
  • 調(diào)用時(shí),生成該數(shù)據(jù)類型類型的一個(gè)實(shí)例

可變對(duì)象的工廠函數(shù)

  • list()
  • set()
  • dict()

列表

old_list = [1, 2, [3, 4]]
new_list = list(old_list)

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))

集合

old_set = {1, 2, 3}
new_set = set(old_set)

old_set.add(4)

print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set))


# 輸出結(jié)果
Old set: {1, 2, 3, 4} old set id: 4484723648
new set: {1, 2, 3} new set id: 4484723872

字典

old_dict = {"name": "小明"}
new_dict = dict(old_dict)

old_dict["second"] = "Python"

print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict))


# 輸出結(jié)果
Old dict: {'name': '小明', 'second': 'Python'} old dict id: 4514161536
new dict: {'name': '小明'} new dict id: 4515690304

方式三:使用數(shù)據(jù)類型自帶的 copy 方法

列表

old_list = [1, 2, [3, 4]]
new_list = old_list.copy()

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))


# 輸出結(jié)果
Old list: [1, 2, [100, 4], 5] old list id: 4309832000  old list[0] id: 4310372992
new list: [1, 2, [100, 4]] new list id: 4309735296  new list[0] id: 4310372992

集合

old_set = {1, 2, 3}
new_set = old_set.copy()

old_set.add(4)

print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set))


# 輸出結(jié)果
Old set: {1, 2, 3, 4} old set id: 4309931392
new set: {1, 2, 3} new set id: 4309930944

字典

old_dict = {"name": "小明"}
new_dict = old_dict.copy()

old_dict["second"] = "Python"

print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict))

 

# 輸出結(jié)果
Old dict: {'name': '小明', 'second': 'Python'} old dict id: 4308452288
new dict: {'name': '小明'} new dict id: 4308452224

源碼

    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of the list. """
        pass

已經(jīng)寫的很清楚,這是淺拷貝

方式四:使用 copy 模塊的 copy 方法

列表

from copy import copy

old_list = [1, 2, [3, 4]]
new_list = copy(old_list)

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))


# 輸出結(jié)果
Old list: [1, 2, [100, 4], 5] old list id: 4381013184  old list[0] id: 4381159936
new list: [1, 2, [100, 4]] new list id: 4381012800  new list[0] id: 4381159936

集合

from copy import copy

old_set = {1, 2, 3}
new_set = copy(old_set)

old_set.add(4)

print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set))


# 輸出結(jié)果
Old set: {1, 2, 3, 4} old set id: 4381115552
new set: {1, 2, 3} new set id: 4381115776

字典

from copy import copy

old_dict = {"name": "小明"}
new_dict = copy(old_dict)

old_dict["second"] = "Python"

print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict))


# 輸出結(jié)果
Old dict: {'name': '小明', 'second': 'Python'} old dict id: 4381159680
new dict: {'name': '小明'} new dict id: 4379632576

到此這篇關(guān)于Python中淺拷貝的四種實(shí)現(xiàn)方法小結(jié)的文章就介紹到這了,更多相關(guān)Python 淺拷貝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Python 實(shí)現(xiàn)打印單詞的菱形字符圖案

    Python 實(shí)現(xiàn)打印單詞的菱形字符圖案

    這篇文章主要介紹了Python 實(shí)現(xiàn)打印單詞的菱形字符圖案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • python利用 pytesseract快速識(shí)別提取圖片中的文字((圖片識(shí)別)

    python利用 pytesseract快速識(shí)別提取圖片中的文字((圖片識(shí)別)

    本文介紹了tesseract的python調(diào)用,也就是pytesseract庫(kù),其中還有一些其他的內(nèi)容并沒(méi)有涉及,僅涉及到了圖片提取文字,如果你對(duì)其感興趣,可以深入探索一下,也希望能和我探討一下
    2022-11-11
  • Python實(shí)現(xiàn)向PPT中插入表格與圖片的方法詳解

    Python實(shí)現(xiàn)向PPT中插入表格與圖片的方法詳解

    這篇文章將帶大家學(xué)習(xí)一下如何在PPT中插入表格與圖片以及在表格中插入內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-05-05
  • 在Tensorflow中實(shí)現(xiàn)leakyRelu操作詳解(高效)

    在Tensorflow中實(shí)現(xiàn)leakyRelu操作詳解(高效)

    這篇文章主要介紹了在Tensorflow中實(shí)現(xiàn)leakyRelu操作詳解(高效),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • 基于Python+QT的gui程序開發(fā)實(shí)現(xiàn)

    基于Python+QT的gui程序開發(fā)實(shí)現(xiàn)

    這篇文章主要介紹了基于Python+QT的gui程序開發(fā)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 解讀Python中字典的key都可以是什么

    解讀Python中字典的key都可以是什么

    這篇文章主要介紹了解讀Python中字典的key都可以是什么,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Django xadmin安裝及使用詳解

    Django xadmin安裝及使用詳解

    這篇文章主要介紹了Django xadmin安裝及使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • python實(shí)現(xiàn)zencart產(chǎn)品數(shù)據(jù)導(dǎo)入到magento(python導(dǎo)入數(shù)據(jù))

    python實(shí)現(xiàn)zencart產(chǎn)品數(shù)據(jù)導(dǎo)入到magento(python導(dǎo)入數(shù)據(jù))

    這篇文章主要介紹了python實(shí)現(xiàn)zencart產(chǎn)品數(shù)據(jù)導(dǎo)入到magento(python導(dǎo)入數(shù)據(jù)),需要的朋友可以參考下
    2014-04-04
  • python中requests庫(kù)+xpath+lxml簡(jiǎn)單使用

    python中requests庫(kù)+xpath+lxml簡(jiǎn)單使用

    這篇文章主要介紹了python中requests庫(kù)+xpath+lxml簡(jiǎn)單使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 利用python程序生成word和PDF文檔的方法

    利用python程序生成word和PDF文檔的方法

    這篇文章主要給大家介紹了利用python程序生成word和PDF文檔的方法,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家具有一定的參考價(jià)值,有需要的朋友們下面來(lái)一起看看吧。
    2017-02-02

最新評(píng)論