Python: Why does the following program Delete only one zero?

Source: Internet
Author: User
S [,] forxins: ifx0s. remove (x) Why is the result [, 3] instead? S = [0, 0, 5, 3]
For x in s:
If x = 0
S. remove (x)
Why is the result [, 3] instead? Reply content: initial s [0] = 0, s [1] = 0, s [2] = 5, s [3] = 3

When the for command is executed for the first time,
X = s [0], that is, x = 0 and then remove this element,
S is changed to [0, 5, 3], that is, s [0] = 0, s [1] = 5, s [2] = 3
For the second execution,
X = s [1], that is, x = 5
Because x is not 0, s remains unchanged.

The reason for this result is that list s has changed during the operation.

A reasonable way is to place elements not 0 in a new list.

print [x for x in s if x != 0]
During the loop, try not to add or delete the list. Do not change the array in the loop. I do not want to say anything important three times. Locke is right. It's not easy. Let's share my opinion.
In for x in s: s is the list of your iterations, and in iteration, you use s. remove (x), this line of code modifies the original list s, so that the second execution of "for x in s:", Here s and the first execution Not the same listThe first time s = [0, 0, 5, 3], the second time s = [0, 5, 3], but the subscript they use is the same group of subscript, that is, if the first subscript is 0 and the second subscript is 1, then the list s corresponding to the two times shows that the first s [0] is 0, the second s [1] is 2 (corresponding to the second s)
Therefore, this easy mistake is that the list of iterations has changed, so in python iteration, Do not modify the list(Here is a mistake you will make in the future -- List variability and multi-Reference, Which will change the original list when modifying other lists)
Then, except for Locke's List push(You must know the best, most distinctive, and most X-Day bombing skills in python). Other solutions:
1.) create a new list result = [] and save the result. append (x) value that meets the conditions. In this way, the original list is not modified.
2 .) use a = s, iterate a, for x in a:, and then use s. remove (x) modify the list s. Congratulations, you are wrong, that is, the multi-reference problem mentioned above. Please refer to this document to understand it.
If you do not understand it, share a sentence" The book has been read for hundreds of times."@ Locke's explanation is correct, but the method can be improved.
This is more suitable for the Python filter:

#!/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(lambda x: x != 0, s))
S = [I for I in s if I! = 0] # cell phone code word, take a look
There are good examples in the official document, which is in the list section of version 2.7.
When looping, the original column a [] is not used, but a [:] is used.
This method is the easiest and requires no additional code.
In addition, @'s problem is very good, and @ Locke's explanation is correct. Because the for loop does not obtain the length of a single column first, the efficiency is too low. For is implemented through the iter iterator, so it is a dynamic iteration by subscript until the end.
Life is short, I use python. s is no longer the original s. If x is indeed the original x, it will be executed four times in a loop. The fourth time should be because the array is out of bounds. Is it true that Mao does not report an error? How does python check the mechanism?

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.