In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to achieve deep and shallow copy of Python", the content is simple and clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve deep and shallow copy of Python" this article.
I. shallow copy
Shallow copy, remember one thing, shallow copy no matter how complex, only copy the first layer, look at the picture and talk.
1. Define the source list list_s, list list_c, and copy to see that the values of the two lists are equal, as shown in the following figure.
2. Now the source list list_s data changes, increasing the value list_s.append (3), the list_s value changes, and the value of the list list_c remains the same, as shown in the following figure.
3. Now the list_c data changes, the value list_c.append (4) is increased, the list_c value changes, and the value of the source list list_s remains the same, as shown in the following figure.
As can be seen from the above three steps, there is a list of [3jue 4] nested in the source list_s, and 3 is appended to the source list_s, and it is found that the list_s value has changed, but the list_c value has not changed, while 4 has been appended to list_c, and it is found that the list_s value has not changed, while the list_c value has changed. Then change the [3jue 4] in the source list_s to see how the list_s and list_c values will change, and move on.
4. Modify the value of [3Power4] in the source list_s, increase it by 5, and change it to [3magin4]. It is found that the source list_s has changed, and the list_c value has also changed. Moreover, the two changes are the same, and both become [3Power4], as shown in the following figure.
5. Modify the value of [3Power4] in list_c, increase by 6, and change into [3Magne4]. It is found that when the list_c changes, the source list_s value also changes, and the two changes are the same, as shown in the following figure.
As can be seen from the above two steps, both the source list_s and the list_c change the same when either the source list_s or the list_c changes the value of the nested list [3pr 4] in the list.
From the memory address, as shown in the following figure.
Green is the additional content, you can see that after the shallow copy, the content and address pointed to by the nested list [3jue 4] in the list have not changed, so, no matter what changes occur in [3jue 4], llist_s and list_c will have the same change, while the list on the first layer, list_s and list_c are independent of each other, and the changes between the two will not affect each other.
To sum up, shallow copies only copy the first layer, and deep data changes will affect others. Second, deep copy
After understanding the shallow copy, the deep copy is easy to understand. Deep copy is to open up a new space in memory, no matter how complex the data structure is, as long as the data changes, it opens up a new memory space to copy the content down to the last layer, and the popular point is, deep copy is to redefine a variable, which is not related to the previous one, so change the content inside. The original will not change. As an example, as shown in the following figure.
Define list_s= [1jue 2, [3jue 4]], while list_c makes a deep copy, the initial values are the same, and then add 3 to list_s and 5 to the list [3jue 4] in the columns in list_c. It is found that the changes of list_s [1jue 2, [3jue 4], 3] and list_c [1jue 2, [3jue 4]] do not affect each other.
The above is all the contents of the article "how to make a deep and shallow copy of Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.