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

问题描述 投票:0回答:1

我觉得这个答案可能很简单,但我需要一些帮助。我需要从三个句子的文档字符串创建一个包含 3 个句子的列表。我最初认为可以使用 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)```
python list loops methods
1个回答
0
投票
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?']
© www.soinside.com 2019 - 2024. All rights reserved.