人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2021-12-14 08:30 | 文章來源:CSDN

方式一:使用切片 [:]

列表

# 淺拷貝 [:]
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]))

# 輸出結果
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ù)簡介

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

可變對象的工廠函數(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))

# 輸出結果
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))

# 輸出結果
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]))

# 輸出結果
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))

# 輸出結果
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))
 
# 輸出結果
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]))

# 輸出結果
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))

# 輸出結果
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))

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

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

版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

相關文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部