某些项目未被删除

问题描述 投票:0回答:1

我有remove()的问题。我必须读取一个字符串并将其转换为列表,然后此列表我必须删除项目"",但最后的项目不会被删除。我给你看了代码:

def telegrama(texto):
    c_aux = texto.split(" ")
    print(c_aux)

    for i in c_aux:
         if i == "":
         c_aux.remove(i)
    print(c_aux)

texto = "  Llego mañana alrededor del mediodía "

telegrama(texto)

我展示了结果

enter image description here

python python-3.x items
1个回答
2
投票

首先应用strip()

c_aux = texto.strip().split(" ")

str.strip()返回字符串的副本,删除了前导和尾随空格。

如果您需要从左侧或右侧移除,您可以分别使用lstrip()rstrip()

© www.soinside.com 2019 - 2024. All rights reserved.