Python: Why does the following program delete only one 0?

Source: Internet
Author: User
s=[0,0,5,3]
For x in S:
If x==0
S.remove (x)
What is the result [0,5,3] instead of [5,3]?

Reply content:

Initial s[0] = 0, s[1] = 0, s[2] = 5, s[3] = 3

For the first time for execution,
x = S[0], which is x = 0 and then the element is removed,
s becomes [0, 5, 3], i.e. s[0] = 0, s[1] = 5, s[2] = 3
For execution the second time,
x = s[1], i.e. x = 5
Because x is not 0,s unchanged

The cause of your result is that the list S has changed during the operation.

The logical approach is to put elements that are not 0 back into a new list.

print[xforxinsifx!=0]
In the loop, try not to increase the list, delete operation in the loop in the array to change the operation, the important thing I do not want to say three times. Locke's solution, but preface, share my opinion.
In the for X in S: S is the list of your iterations, and in the iteration you use S.remove (x), which modifies the original list s so that the second time you execute "for x in S:", here s and the first executed not the same list, for the first time s = [0, 0, 5, 3], second s = [0, 5, 3], but they use the subscript is the same set of subscript, that is, the first subscript is 0, the second subscript is 1, then the corresponding two times the list of S, that the first s[0] is 0, the second s[1] 2 ( Corresponds to the second s)
So, this is an easy mistake because the list of iterations has changed, so in the Python iteration, Be sure not to modify the list(Here's a mistake you'll make later-- variability and multiple references for lists, causing the problem of changing the original list when modifying the other list)
Well, besides Locke's list Push- down(Python is best used, the most distinctive, the most X-fried days of the technique, you have to be OH), Other solutions:
1.) Create a new list result = [], save the qualifying value Result.append (x) so that the original list is not modified
2.) Use a = s, then iterate A, for x in a:, and then use S.remove (x) to modify the list s, then congratulations, you are wrong, is the above mentioned multi-citation problem, this yourself to read the document to understand it
Finally, if you don't see it, share a word. the book reads The Times, its righteousness self-see"@ Locke's explanation is correct, but the method can be improved.
This is actually more appropriate for the Python filter to solve:

#!/usr/bin/env Python3def Check_number(x):    if x == 0:        return False    Else:        return Trues = [0,0,5,3]Print(Filter(Check_number,s))
s=[0,0,5,3]print(filter(lambdax:x!=0,s))
s = [I for I in S if I! = 0] #手机码字, fight, see.
The official documentation has a good example, in the 2.7 version of the list section.
The loop is not useful for the original list a[], but instead uses a[:]
This method is the simplest and does not require additional code to be added.
In addition, @ Black son's problem is very good, @ Locke's explanation is right. Because the For loop does not get the column length first, it is too inefficient. For is implemented by the ITER iterator, so it is dynamically pressed down the iteration until the end.
Life was short, I use python.s already is not the original s,x really original X such explanation, the loop experience executes four times, that fourth time should be array out of bounds, for hair not error? What is the check mechanism of Python?
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.