Quote: This question originated from a netizen’s request for help. As the Dragon King of the Water Group, it is my duty to take this question. Let’s look at the question first:
Get this question, the meaning of the question is very clear, the elements in the list containing 2 need to be deleted, and then output the deleted new list. The first idea I thought of was to use the for loop to traverse the string, use the string operator x in s (if x is a substring of s, return True, otherwise return False), use the if function, and delete if it is True (remove) The element. Finally, the new list is output.
The idea code and running results are as follows (this is wrong)
Error instance
Observe the output result. Compared with the original list, although some elements containing 2 are eliminated, they are not completely eliminated. Why?
Let's analyze that python traverses according to the subscripts of the list elements. Therefore, the subscript of element 123 is 1, and the subscript of element 212 is 2. S.remove is executed in the first loop, and element 123 is deleted. When entering the second loop! ! ! Here comes the point. Note that the element 123 has been deleted at this time, so the subscript of the element 212 has changed from 1 to 0, and the subscript of the element 434 has changed from 2 to 1. The subscript 0 has been executed in the first loop, so the second loop will look for the element with the subscript 1.
At this point, you must already know why 212 was not deleted in the output result, because these two items were not discovered by python at all, and they sat on the position of the previous element and escaped routine inspection. Why is element 1215 not deleted? ? Because its subscript becomes the position of the previous element 231, it has escaped the routine check of the python brother.
Ok, the cause of the error has been found, how to solve it? Traversal is like a road. You can walk from the beginning to the end of the road, or from the end of the road to the beginning. Just now we were traversing in positive order, and it couldn't solve the problem. What about the other way around? try it.
The structure of reverse traversal can be like this: for i in range(len(s)-1,-1,-1)
The code and running results are as follows (this is correct)
Correct example
You can see that we have got the results we want to output. There is no more explanation about why the reverse order does not appear here (because I don’t know how to explain hahaha). Just remember to use the for loop to traverse and delete elements. Traverse forward, otherwise the list will be out of bounds.
Do we have other options besides using the remove method? Of course, there are some pythons, but not many other functions.
There are several ways to implement list reverse traversal (and more):
Summary of various methods
So far, this article on the implementation of reverse traversal of python lists is introduced here. For more relevant content of reverse traversal of python lists, please search ZaLou.Cn
Recommended Posts