Questions about .pop () to traverse the list

Learning list .pop () method, to realize the function: through a list of every element inside, and then delete the last element, and print the current list, as shown below:

Function want to achieve

Later want to use for loop iterates print, the results shown:

Function want to achieve

The for loop can not completely remove the elements of the list! Really I do not know what the reason is, in large Xiangqun God for advice.

One of the great God posted the link:

https://stackoverflow.com/questions/13939341/why-does-a-for-loop-with-pop-method-or-del-statement-not-iterate-over-all-list

Ran and eggs, do not understand ah!

Another great God spoke:

The first guest takes guests [0], for one cycle after the last guests element is removed, a reduced number of elements in the original list. Second cycle time is reduced in value in a list of guests, while the second is taken from the guests guests [. 1], and so on, when guests [guest] values ​​fail to bounce back to cycle.

Pretend to understand. . .

Finally, God put a big procedural code, very intuitive. . . However, I do not understand. . . I am helpless ah. . .

Function want to achieve

Function want to achieve

No need to get to the bottom of this problem, has a relationship with python design mechanism itself, if we must get to the bottom to see the python assembly code generated class. The conclusion is not to write a similar change in the outer layer of the inner code.

You can only write down, and so will we be able to see to understand. . .

Guess you like

Origin www.cnblogs.com/neoj323/p/11605413.html