- When I was writing code recently, the script couldn't run no matter what. After checking for a long time, it turned out that I made a low-level mistake.
- The value of the original list needs to be copied in the script, and I directly use "=" to assign the value. The code example is as follows:
list_old = [1,2,3] list_new = list_old
- The result of this is that when I try to modify the new list, the values of the original list will also be modified.
- The correct way should be to use a method that just copies the list values.
- This article records the various methods of copying the list. It can be regarded as a summary and a reminder to myself. I hope that I will not make such low-level mistakes again.
Article directory
1. Direct assignment
- This method is the case above
list_old = [1,2,3] list_new = list_old
- In the case of direct assignment, both the original list and the new list point to the same list object.
- For example, add an element to the new list, print the old list data and you will find that an element is also added
Second, the list copy method ( list.copy() )
-
This method will only copy the values inside the list
list_old = [1,2,3] list_new = list_old.copy()
-
The old and new lists point to two list objects with the same value
-
Adding an element to the new list leaves the old list unaffected.
3. Restructure the list
- The next few methods are relatively similar, all of which take out the data in the old list and reconstruct it into a new list
- So just make a brief introduction and choose the appropriate method.
1. Utilize list index
list_old = [1,2,3]
list_new = list_old[:]
2. Use list() to construct a list
list_old = [1,2,3]
list_new = list(list_old)
3. Use extend() to construct a list
extend() function : Append multiple values in another sequence at the end of the list at once
list_old = [1,2,3]
list_new = []
list_new = list_new.extend(list_old)
4. Use for + append()
list_old = [1,2,3]
list_new = []
for i in list_old:
list_new.append(i)
Four, deep copy copy.deepcopy()
- Note: The above method is just a shallow copy, in simple terms, only one layer of the list will be copied.
- for example:
list_old = [1,2,3,[4,5,6]] list_new = list_old.copy()
- In this case, only the first list elements are copied, but if the operation is performed on the second-level list elements "[4,5,6]", it will affect both the old and new lists.
- At this time, in the old and new lists, the second-level elements still point to the same list "[4,5,6]"
- Then you need to use deep copy
import copy list_old = [1,2,3,[4,5,6]] list_new = copy.deepcopy(list_old)
- In this way, the element "[4,5,6]" of the new list will not be affected by the old list
- In this way, the element "[4,5,6]" of the new list will not be affected by the old list
——————————————————————————————————————————————————————————————————————————————————————————————————
_ [Necessary Skills for Software Testing] I will upload test-related materials from time to time, you can click on the QR code below the article to get it~