Implementation of reverse traversal of python list

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

Implementation of reverse traversal of python list
In-depth understanding of python list (LIST)
Python implementation of gomoku program
Detailed implementation of Python plug-in mechanism
Python implementation of IOU calculation case
Python preliminary implementation of word2vec operation
Implementation of python selenium operation cookie
Python3 list
Implementation of python3 registration global hotkey
Implementation of python student management system
Implementation of python gradient descent algorithm
Basic analysis of Python turtle library implementation
Implementation of JWT user authentication in python
Python implementation of intersection and IOU tutorial
Implementation principle of dynamic binding of Python classes
7 features of Python3.9
python list learning
[902] python list sort
Implementation of Python headless crawler to download files
Python implementation of AI automatic matting example analysis
Python implementation of hand drawing effect example sharing
Implementation of business card management system with python
Python writes the game implementation of fishing master
Implementation of business card management system based on python
Detailed explanation of the implementation steps of Python interface development
Basics of Python syntax
How to understand a list of numbers in python
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
09. Common modules of Python3
Binary tree traversal (Python)
Consolidate the foundation of Python(7)
Subscripts of tuples in Python
Python analysis of wav files
Consolidate the foundation of Python(6)
Python drawing rose implementation code
Analysis of JS of Python crawler
python king of glory wallpaper
Python basic syntax list production
Consolidate the foundation of Python(5)
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Analysis of Python object-oriented programming
Implementation of CentOS8.0 Network Configuration
Python version of OpenCV installation
Python GUI simulation implementation calculator
9 feature engineering techniques of Python
matplotlib of python drawing module
Python method of parameter passing
Consolidate the foundation of Python (3)
Collection of Python Common Modules