如何在Python中创建一个包含三句文档字符串的列表,每个元素是一句及其标点?

1 投票
1 回答
25 浏览
提问于 2025-04-12 18:51

我觉得这个问题可能很简单,但我需要一些帮助。我想从一个包含三句话的文档字符串中创建一个句子列表。我最开始想用split()方法来实现,但使用split的一个问题是我需要保留每句话的所有部分。我需要保留所有的空格和标点,所以我理解的split()里的参数不能是标点符号或空字符串。你能帮我创建一个列表,让每个元素都是一句话吗?这是我目前写的代码:

sentence = ''
docstring = 'The rain in #Spain in 2019, rained "mainly" on the plain.\
There is a nice function to split a string into a list based on a given \
delimiter! Why do I try to do too much?'

for character in docstring:
    sentence += character
    if character == '.' or character == '?' or character == '!':
        sentencelist.append(sentence)```

1 个回答

2
docstring = 'The rain in #Spain in 2019, rained "mainly" on the plain. There is a nice function to split a string into a list based on a given delimiter! Why do I try to do too much?'

sentencelist = re.split(r'(?<=[.!?])\s+', docstring)
print(sentencelist)

与其一个一个字符地处理,不如直接用正则表达式来解决这个问题。为了保留这些结果,我们在正则表达式中使用了正向查找的技巧。

Output:
['The rain in #Spain in 2019, rained "mainly" on the plain.', 'There is a nice function to split a string into a list based on a given delimiter!', 'Why do I try to do too much?']

撰写回答