Cheap VPS host selection
Provide server host evaluation information

Python traversal loop (python loop statement)

As we all know about loop traversal, it is nothing more than for and while. Today I write about different loops and traversals here. In practice, we sometimes encounter the problem of deleting elements in the list. What should we do when iterating through the list to delete the specified elements?

Let's go directly to the code to see the case:

 import time #Delete all Zhang surname elements in the following list, and the output result should be ['Boss Li ',' Second Li '] lst = [ 'Boss Zhang' , 'Zhang Laoer' , 'Boss Li' , 'Zhang Laosan' , 'Li Laoer' ]* ten thousand

 #Directly for loops through the list and removes the elements to be deleted
 def  del1 (lst) :
   for i in lst: if i[ zero ] == 'Pieces' : lst.remove(i) #When deleting lst [0] 'Boss Zhang', the list length becomes 4, resulting in that lst [1] takes the value of 'Boss Li' and skips' Boss Zhang '
   return lst #The returned result is not as expected


 #Forward traversal, by creating a copy of the original list, and then traversing the copy, delete the elements in the original list
 def  del2 (lst) : lst2 = lst.copy() #High memory and time consumption for creating replicas
   for i in lst2: if i[ zero ] == 'Pieces' : lst.remove(i) #Delete the first matching element, and retrieve the matching time is expensive
   return lst #Although the result is correct, the efficiency is extremely low. Do not use this method


 #Use higher-order function filter method
 def  del3 (lst) :
   def  comp (n) :  #Create filter function
     return n[ zero ] != 'Pieces'  #For elements whose first character is not 'sheet', return True and keep it. If false is returned, it will be deleted.
   return list(filter(comp, lst)) #The filter higher-order function deletes the elements in the list,
 #The deletion condition is the comp method, and the iterator is returned. The list method needs to be converted into a list


 #Reverse order deletion method
 def  del4 (lst) :
   for i in range(len(lst) - one , -1 , -1 ): #Note that len (lst) must be - 1, because the subscript of the list element is 0 to len (lst) - 1; Pay attention to the left opening and right closing of the for loop,
 #It must be - 1 from the end of the lst queue to the beginning. Writing 0 will miss lst [0]- 1 indicates reverse order. Range is actually an int number list generator. What is actually generated here is
 #[4999949998,.. 1,0], access the specified elements of the list through subscripts.
     if lst[i][ zero ] == 'Pieces' : del lst[i] return lst #The while loop deleted in reverse order has the same effect as the for sequence number, and the difference in operating efficiency is extremely small (the memory cost of the for sequence number method is slightly higher). The while loop needs to write 7 lines,
 #The for loop only needs 5 lines. It is more recommended to use the for loop. But while loop code is easier to read.
 def  del5 (lst) : length = len(lst) - one
   while length >= zero : if lst[length][ zero ] == 'Pieces' : del lst[length] length -= one
   return lst #Lst=del1 (lst) # The del1 method directly traverses the list to delete the specified element, and the returned result is incorrect
 # print(lst)

 # t1 = time.time()
 #Lst=del2 (lst) # The del2 method creates a copy of the original list and traverses the copy to delete the specified elements in the original. The returned results are correct, but the operation efficiency is extremely low
 # t2 = time.time()
 #Print (f "The traversal method takes time to delete elements: {t2 - t1:. 5f}") # 4.51529. You can see the necessity of code optimization here. Although the running results are consistent, the performance varies greatly.
 #The performance is generally evaluated by two indicators, one is time consumption, and the other is resource consumption (usually memory consumption, and other resource consumption in special occasions). t1 = time.time() lst = del3(lst) t2 = time.time() print( F "When the filter method deletes an element: {t2 - t1: .5 f} " ) # 0.00596

 # t1 = time.time()
 # lst = del4(lst)
 # t2 = time.time()
 #Print (f "The traversal method takes time to delete elements: {t2 - t1:. 5f}") # 0.07991

 # t1 = time.time()
 # lst = del5(lst)
 # t2 = time.time()
 #Print (f "The traversal method takes time to delete elements: {t2 - t1:. 5f}") # 0.08516

The above case notes are very detailed, and beginners can read them for reference. The time consumption of del4() for loop and del5() while loop is almost the same, and the memory consumption of for loop is slightly higher. The for loop can write two fewer lines of code than the while loop. Learning python and using python under the same performance indicators naturally means that the fewer the lines of code, the better. For is the priority for writing loops.

Do not reprint without permission: Cheap VPS evaluation » Python traversal loop (python loop statement)