gpt4 book ai didi

python - 删除python中的所有标点符号

转载 作者:行者123 更新时间:2023-11-28 21:42:15 41 4
gpt4 key购买 nike

<分区>

作为更大程序的一部分,我需要删除字符串中的所有标点符号。当我像这样为每个标记单独编写它时,它正在工作:

words = [word.replace(".", "") for word in words]

但是当我尝试在循环中执行此操作时,它不起作用。

line = "I was going to leave her, but in the very last moment I had changed 
my mind. Interesting thing, many nice ways to use."
words = line.lower().split()
for punc in [".",","]:
if punc in words:
words = [word.replace(punc, "") for word in words]
print words

你能告诉我,我做错了什么吗?

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com