我觉得这个答案可能很简单,但我需要一些帮助。我需要从三个句子的文档字符串创建一个包含 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)```
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?']