我需要将我的单词和标点符号分开- 我正在考虑让该函数查看每个单词并确定其中是否有标点符号,方法是从 [-1] 开始,索引为 -1,然后在单词遇到字母时立即将其与标点符号分开而不是标点符号...
sent=['I', 'went', 'to', 'the', 'best', 'movie!','Do','you', 'want', 'to', 'see', 'it', 'again!?!']
import string
def revF(List):
for word in List:
for ch in word[::-1]:
if ch is string.punctuation:
#go to next character and check if it's punctuation
newList= #then split the word between the last letter and first puctuation mark
return newList
最佳答案
如果您只想从字符串中删除标点符号。 Python 提供了更好的方法来做到这一点 -
>>> import string
>>> line
'I went to the best movie! Do you want to see it again!?!'
>>> line.translate(None, string.punctuation)
'I went to the best movie Do you want to see it again'
关于Python反向迭代分割单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17714141/