如何将字符串列表转换为句子列表?

时间:2018-11-01 16:28:06

标签: python string split

我正在尝试将字符串列表(文本块)转换为小写并将文本转换为句子列表,其中句子是由句号分隔的字符串。例如:

['This is some text',
 'that I have. But it',
 'is formatted like this.']

我想使每个句子一个字符串(以及所有小写字母)。目前,我有以下内容:

def make_sentences(text):

    newstring = ''
    for string in text:       
        newstring += str(string.lower()) + ' '
    newstring = newstring.split('.')

    return newstring

这可以完成工作,但是现在有些单词跨越了两行(见下文)。有没有更好的方法来解决此问题以防止这种情况发生?

['my current output lo
 oks like this.']

非常感谢

1 个答案:

答案 0 :(得分:1)

我建议使用内置的.join()方法,然后通过.split() '. '对其进行修饰:

def make_sentences(text):
    return ' '.join(text).lower().split('. ')

示例输出:

sample = [
    'This is some text',
    'that I have. But it',
    'is formatted like this.'
]
make_sentences(sample)

>>>['This is some text that I have.', 'But it is formatted like this.']

PS

我在写这篇文章时也注意到了,但是没有指出。但是在您的字符串中,您还有一个额外的报价

相关问题