日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 中浅拷贝的四种实现方法

發布時間:2025/3/20 python 7 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 中浅拷贝的四种实现方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

方式一:使用切片 [:]

列表

# 淺拷貝 [:] old_list = [1, 2, [3, 4]] new_list = old_list[:]old_list.append(5) old_list[2][0] += 97print("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

方式二:使用工廠函數

工廠函數簡介

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

可變對象的工廠函數

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

列表

old_list = [1, 2, [3, 4]] new_list = list(old_list)old_list.append(5) old_list[2][0] += 97print("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

字典

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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

方式三:使用數據類型自帶的 copy 方法

列表

old_list = [1, 2, [3, 4]] new_list = old_list.copy()old_list.append(5) old_list[2][0] += 97print("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

集合

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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

已經寫的很清楚,這是淺拷貝

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

列表

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' from copy import copyold_list = [1, 2, [3, 4]] new_list = copy(old_list)old_list.append(5) old_list[2][0] += 97print("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 copyold_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 copyold_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有幫助!

Python基礎入門教程推薦

Python爬蟲案例教程推薦

總結

以上是生活随笔為你收集整理的Python 中浅拷贝的四种实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。