我发现了这个问题,要求对数组进行就地修改,以便将所有零移动到数组的末尾,并保持非零元素的其余顺序。根据问题陈述,就地意味着无需复制原始数组。 (摘自Leetcode,可以找到#283,Move Zeroes)。

输入和输出的示例为[0,1,0,13,12]变为[1,13,12,0,0]。我看到的一个简单解决方案是:

for num in nums:
    if num == 0:
        nums.remove(num)
        nums.append(0)


该解决方案清晰易懂,因此可以理解,它可以完成预期的工作。

但是,我在就地部分上并不完全清楚/出售,因为我不确定remove在后台如何工作。在内部删除是否会复制数组以删除指定的元素-它如何工作?使用“就地”这一概念,下面我的初始解决方案是否就地考虑(因为它不会复制nums,而是修改了nums的原始版本)?

indices = []
for en, num in enumerate(nums): # get the index of all elements that are 0
    if num == 0:
        indices.append(en)

for en, i in enumerate(indices):
    new_i = i-en # use the index, accounting for the change in length of the array from removing zeros
    nums = nums[:new_i] + nums[new_i+1:] # remove the zero element
nums = nums + [0] * len(indices) # add back the appropriate number of zeros at the end

最佳答案

是否在内部删除数组的副本以删除指定的元素?

没有

它是如何工作的?

从python source code for lists中,remove()调用listremove()

listremove(PyListObject *self, PyObject *v)
{
    Py_ssize_t i;

    for (i = 0; i < Py_SIZE(self); i++) {
        int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
        if (cmp > 0) {
            if (list_ass_slice(self, i, i+1, (PyObject *)NULL) == 0)
                Py_RETURN_NONE;
            return NULL;
        }
        else if (cmp < 0)
            return NULL;
    }
    PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
    return NULL;
}


Python将列表切成要删除项目的索引。我发现了对删除功能here的更好描述:

arguments: list object, element to remove
returns none if OK, null if not
listremove:
    loop through each list element:
        if correct element:
            slice list between element's slot and element's slot + 1
            return none
    return null


python - 在python中就地修改数组-LMLPHP

有了您的“就地”概念,我想说它已经就位,并且适合您的特定情况。但是有人已经注意到,在迭代列表时不要修改列表,这已经成为一个很好的观点。

07-28 08:56