for cycle time pop phenomenon

= A [ ' A ' , ' B ' , ' C ' , ' D ' ,. 1, 2,. 3, 4,5,6,7,8 ]
 for index, X in the enumerate (A):
     Print ( " Index is {}, {} is the length of the list, delete the values {} " .format (index, len (a), a [-1 ])) 
    a.pop () 
    Print (a) 

    Print ( " = " * 20 is ) 

Print ( ' ~~~~~ ' ) 

A2 = [ ' A ' ,'b','c','d',1, 2, 3, 4,5,6,7,8]
for i in a2:
    a2.pop()
    print(a2)

 

`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

/usr/local/bin/python3.7 /code/pop2.py
index is 0, the length of the list is 12, the value is deleted. 8
[ 'A', 'B', 'C', 'D',. 1, 2, 3, 4, 5, 6, 7]
====================
index is 1, the length of the list is 11, the value is deleted 7
[ 'a ',' B ',' C ',' D ',. 1, 2,. 3,. 4,. 5,. 6]
====================
index is 2, length of the list is 10, the value is deleted. 6
[ 'a', 'B', 'C', 'D',. 1, 2,. 3,. 4,. 5]
============ ========
index is 3, the length of the list is 9, the value is deleted. 5
[ 'a', 'B', 'C', 'D',. 1, 2, 3,. 4]
== ==================
index is 4, the length of the list is 8, the value deleted is the 4
[ 'a', 'B', 'C', 'D', 1, 2, 3]
====================
index is 5, the length of the list 7, the value is deleted 3
[ 'a', 'B', 'c', 'd', 1,2]
====================
~~~~~
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6, 7]
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6]
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5]
['a', 'b', 'c', 'd', 1, 2, 3, 4]
['a', 'b', 'c', 'd', 1, 2, 3]
['a', 'b', 'c', 'd', 1, 2]

Process finished with exit code 0

 

 

Why the analysis, the final result is not pop [], but the [ 'a', 'b', 'c', 'd', 1, 2] is stopped.

Can look at the first code. When each iteration, the index values ​​are accumulated when the time index is 5, pop has been deleted to [ 'a', 'b', 'c', 'd', 1 2] left 6., index values ​​is six 0,1,2,3,4,5

Assuming that the index is 6, then the list of values ​​which we need seven, but the results have not seven values ​​in it. So it was suspended. 

Therefore, if it continues to accumulate the index, the index is 6, the value of [ 'a', 'b', 'c', 'd', 1,] is the index values, but this in turn 0,1,2,3,4 only five values, the index only up to 4, so there is no index 6, only suspended.

 

 

================= Similarly ===================

= A [. 1, 2,. 3,. 4 ]
 for index, X in the enumerate (A):
     Print ( " index of {}, to delete the value of {} is the length of the list is {} " .format (index, X , len (A))) 
    a.remove (X) 
    Print ( " = " * 20 is )
 Print (A)
 

 

The results are as follows:

the index is 0, the value 1 is to be deleted, the length of the list is 4
====================
index is 1, the value is 3 to delete, 3 is the length of the list
====================
[2, 4]

 

If the index is 2, but the above [2,4], only to index 1, it is 0, 1. 2 index does not exist, therefore, only the results of the 2 and 4. 

 

 

note:

Variation in length of the list, the index value is changing. This is easy to overlook one point, because the object is a for loop iteration may object.

 

 

Therefore, if the above list, recycled to [] may be solved by the following method.

= A2 [ ' A ' , ' B ' , ' C ' , ' D ' , 1,2,3,4,5,6,7,8 ] 

Print (len (A2)) 

for I in Range (len (A2 )):
     # del A2 [0] this method can also be, may be the following pop 
    a2.pop ()
     Print (A2)

 

/usr/local/bin/python3.7 /code/pop2.py
12
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6, 7]
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6]
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5]
['a', 'b', 'c', 'd', 1, 2, 3, 4]
['a', 'b', 'c', 'd', 1, 2, 3]
['a', 'b', 'c', 'd', 1, 2]
['a', 'b', 'c', 'd', 1]
['a', 'b', 'c', 'd']
['a', 'b', 'c']
['a', 'b']
['a']
[]

Process finished with exit code 0

 

Guess you like

Origin www.cnblogs.com/666sss/p/11620024.html
pop