In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the implementation methods of shallow copy in Python". In daily operation, I believe that many people have doubts about the implementation of shallow copy in Python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the implementation methods of shallow copy in Python?" Next, please follow the editor to study!
Method 1: use slices [:]
List
# shallow copy [:] 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]) # output result Old list: [1,2, [100,4], 5] old list id: 4537660608 old list [0] id: 4537659840new list: [1,2, [100,4]] new list id: 4537711424 new list [0] id: 4537659840 method 2: use factory function
Brief introduction of factory function
A factory function looks like a function, but it is actually a class
When called, an instance of the data type is generated
Factory function of mutable object
List ()
Set ()
Dict ()
List
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]))
Set
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)) # output result Old set: {1,2,3,4} old set id: 4484723648new set: {1,2,3} new set id: 4484723872
Dictionaries
Old_dict = {"name": "Xiaoming"} 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) # output result Old dict: {'name':' Xiaoming' 'second':' Python'} old dict id: 4514161536new dict: {'name':' Xiao Ming'} new dict id: 4515690304 method 3: use the copy method that comes with the data type
List
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]) # output result Old list: [1,2, [100,4], 5] old list id: 4309832000 old list [0] id: 4310372992new list: [1,2, [100,4]] new list id: 4309735296 new list [0] id: 4310372992
Set
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)) # output result Old set: {1,2,3,4} old set id: 4309931392new set: {1,2,3} new set id: 4309930944
Dictionaries
Old_dict = {"name": "Xiaoming"} 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)) # output Old dict: {'name':' Xiaoming' 'second':' Python'} old dict id: 4308452288new dict: {'name':' Xiao Ming'} new dict id: 4308452224
Source code
Def copy (self, * args, * * kwargs): # real signature unknown "" Return a shallow copy of the list. "" Pass
It has been written very clearly. This is a shallow copy.
Method 4: use the copy method of the copy module
List
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]) # output result Old list: [1,2, [100,4], 5] old list id: 4381013184 old list [0] id: 4381159936new list: [1,2, [100,4]] new list id: 4381012800 new list [0] id: 4381159936
Set
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)) # output result Old set: {1,2,3,4} old set id: 4381115552new set: {1,2,3} new set id: 4381115776
Dictionaries
From copy import copyold_dict = {"name": "Xiaoming"} 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) # output result Old dict: {'name':' Xiaoming' 'second':' Python'} old dict id: 4381159680new dict: {'name':' Xiao Ming'} new dict id: 4379632576 so far The study on "what are the implementation methods of shallow copy in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.