无法从索引Python 3中删除正确的元素

我正在尝试从整数中删除数字。

除了输入诸如8880888之类的整数外,我的代码似乎可以正常工作。

出于某种原因,在上面带有一个整数的情况下,删除通过中间数字的索引时,但是不会删除正确的索引。

    n = 8880888
    def question(n):
        newlist = [int(x) for x in str(n)] #coverting integer to list
        result = newlist[:]
        y = newlist[5]
        result.remove(y)
    return result

删除第5个元素时,它应返回888088。 但是,相反,我将返回880888。

xiaoxia20008 回答:无法从索引Python 3中删除正确的元素

您正在使用remove()而不是del方法。如果要删除基于索引的元素,则代码应类似于:

n = [8,8,8]

#If you want to remove the 5th element:

del n[5]

请参阅https://www.csestack.org/difference-between-remove-del-pop-python-list/

本文链接:https://www.f2er.com/3157534.html

大家都在问