1

我需要从我的标点符号中分离出我的单词-我正在考虑让函数查看每个单词并通过从 [-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
4

3 回答 3

1

如果您只想从字符串中删除标点符号。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'
于 2013-07-18T03:51:02.953 回答
0

从您的示例中,我了解到您想将字符串拆分为空格。你可以像这样简单地做到这一点:

my_str = "I went to the best movie! Do you want to see it again!?!"
sent   = my_str.split(' ')
于 2013-07-18T03:51:55.087 回答
0

使用包含要拆分的标点符号的正则表达式:

re.split('re.split(r"[!?.]", text)

示范:

>>> import re
>>> re.split(r"[!?.]", 'bra det. där du! som du gjorde?')
['bra det', ' d\xc3\xa4r du', ' som du gjorde', '']
于 2013-07-18T05:53:43.067 回答