Today, because we want to write a loop-deleted program for a list data type (this is the first time for a list operation), but find a singular problem, let's look at the code and Effect:
1 #to initialize a list, I use the list index to make list elements for the convenience of comparison below.2Datas = [0,1,2,3,4]3 4 #print element Groups for easy comparison5 Print(datas)6 7 #using for Traversal8 forDatainchdatas:9 Ten #remove an element from the list One datas.remove (data) A - #Print the deleted array - Print(datas)
Let's look at this piece of code, the final output, according to my previous experience, should be an empty [], but the result is not:
The above results tell us that the above program only deleted three elements, and did not completely delete, why? From the above results, we can see that the deleted element is 1,3,5, from which we can see, originally, is the list in the For loop index problem, we have to track the list in real-time changes in for:
1 #to initialize a list, I use the list index to make list elements for the convenience of comparison below.2Datas = [0,1,2,3,4]3 4 #print element Groups for easy comparison5 Print(datas)6 7 #The record is the number of times for the Loop8index = 19 Ten #record the subscript value of the current loop datas Onei =0 A - #using for Traversal - forDatainchdatas: the - #number of print cycles - Print('\ n This is the first%d loop, Datas is currently in the For loop with the following subscript value:%d, before the datas element is removed:'%(index, i)) - + #print datas list in real time - Print(datas) + A #remove an element from the list at datas.remove (data) - - - #number of cycles +1 -Index + = 1 - in #Index +1 -i + = 1 to + #final list of datas - Print(' \ nThe end of the loop, the last datas element is:') the Print(datas)
The output results are as follows:
As you can see, the datas does not delete an element in the For loop, its index changes, but the index that is traversed in the For loop will always add 1, so this kind of skipping deletion occurs. In this case, the small part of the idea of a method, we first look at the following section of the program:
1 #initialize a list of lists2Datas = [1,3,4,10,5,3,7,6,9,8]3 4 #using for Traversal5 forDatainchdatas:6 7 #now we need to delete even-numbered elements.8 ifData% 2 = =0:9 Ten #Delete an element in a datas One datas.remove (data) A - #final list of datas - Print('\ n The end of the loop, the last datas element is:') the Print(datas)
Let's take a look at the results of this piece of code:
We can see that 4 and 10 are adjacent, 4 subscript is the 2,10 subscript is 3, so when 4 is deleted, the datas subscript 2 element becomes 10, but, at this time for the loop, the datas subscript is 3, so not take 10 this element, which led to 10 were not deleted. So how do we get rid of it? The small part of the practice is this:
1 #initialize a list of lists2Datas = [1,3,4,10,5,3,7,6,9,8]3 4 #Print original array elements for easy comparison5 Print('the elements of the original datas list are:')6 Print(datas)7 8 #define an empty list to store the elements of the datas to be deleted9Datas_del = []Ten One #using for Traversal A forDatainchdatas: - - #now we need to delete even-numbered elements. the ifData% 2 = =0: - - #do not delete directly, will need to delete the elements, stored in Datas_del - datas_del.append (data) + - #Print the Datas_del list to see which elements to delete + Print('\ n Print the Datas_del list to see which elements to delete') A Print(Datas_del) at - #then execute a for loop, traversing the Datas_del list - forDatainchDatas_del: - - #Delete an element in a datas - datas.remove (data) in - #final list of datas to Print('\ n The elements of the datas after deletion are:') + Print(datas)
Now let's execute the following code above, whether we can successfully delete the elements to delete, we look at the results:
From the above results we can see that we have successfully removed the elements we need, and this is how we think of the small part.
Note: Small series Think this method is not good, because more a for loop consumption, small part think there must be a better way, to ask the big God, then to update this document, if you have friends have a better way, you can also in the comment area to tell small series ~
Python3 list uses for traversal, edge loop edge Delete problem