最好的方法。清洁和剥长线?

2024-09-29 19:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

Desired Outcome Is = ["This", "is", "a", "random", "sentence"]

text = "Th,is is a? random!! sentence..."  # Eddied, added comma inside word 

clean_text = text.split()

for clean in clean_text:

    double_clean_text = clean.strip(",.!?")

    print(double_clean_text)

设法清理了,但我怎样才能把它全部恢复到列表中

这是有效的方法吗


Tags: textcleanaddedisrandomthissentencedouble
3条回答

您可以使用列表理解:

print([t.strip(",.!?") for t in text.split()])

您可以执行以下操作:

print(" ".join([clean.strip(",.!?") for clean in clean_text]))

试试这个:

clean_text = text.split()
print([clean.strip(",.!?") for clean in clean_text])

clean_text = text.split()
res = []
for clean in clean_text:
    double_clean_text = clean.strip(",.!?")
    res.append(double_clean_text)
print(res)

相关问题 更多 >

    热门问题