有些项目没有被删除
Some items are not removed
我对 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)
我展示结果
只需先申请strip()
:
c_aux = texto.strip().split(" ")
str.strip()
returns 删除了前导和尾随空格的字符串副本。
如果只需要从左侧或右侧移除,可以分别使用lstrip()
或rstrip()
。
我对 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)
我展示结果
只需先申请strip()
:
c_aux = texto.strip().split(" ")
str.strip()
returns 删除了前导和尾随空格的字符串副本。
如果只需要从左侧或右侧移除,可以分别使用lstrip()
或rstrip()
。