无法将0移动到列表末尾正确的Python

Mic*_*Gee 2 python

所以这里的目标是将所有0移动到数组的末尾,但我偶然发现了这些小行代码中的问题.

当我使用输入时,我得到所需的输出,如下所示:

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Run Code Online (Sandbox Code Playgroud)

但是,每当我使用此输入时:

[0,0,1]
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

[0,1,0]
Run Code Online (Sandbox Code Playgroud)

当我想要输出时:

[1,0,0]
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我认为我正确实现了这个:

class Solution:
def moveZeroes(self, nums):
    """
    :type nums: List[int]
    :rtype: void Do not return anything, modify nums in-place instead.
    """
    for counter in range(len(nums)):
        if nums[counter] == 0:
            nums.pop(counter) #Remove the 0
            nums.append(0) #Add to end. 
            counter-=1 #Check same index just incase adjacent 0
Run Code Online (Sandbox Code Playgroud)

任何输入都表示赞赏.谢谢!

AKX*_*AKX 5

我甚至不打扰手册循环...

def move_zeroes(nums):
    nums[:] = [n for n in nums if n != 0] + [0] * nums.count(0)

x = [0,1,0,3,12]
move_zeroes(x)
print(x)
Run Code Online (Sandbox Code Playgroud)

输出

[1, 3, 12, 0, 0]
Run Code Online (Sandbox Code Playgroud)