我有下面的字符串,我想把它解析成单词。这是一个连续的句子
text = "If it is hot , don’t touch"
到目前为止,我所尝试的:
import string
text = "If it is hot , don’t touch"
words = [word.replace(',', '') for word in text.split()]
print(words)
然而,我得到了以下结果:
['If', 'it', 'is', 'hot', '', 'don’t', 'touch']
因此,我想要的是:
['If', 'it', 'is', 'hot', 'don’t', 'touch']
您可以使用筛选函数,而不是将“,”替换为“”
可以使用只返回不等于“,”的单词的函数 像这样:
相关问题 更多 >
编程相关推荐