Python如何在列表中拆分字符串

时间:2014-11-05 20:07:49

标签: python string list split

这是我目前的代码:

def poisci_pare(besedilo):
    import re
    seznam = re.split("[.]", besedilo)
    return seznam

返回(我们假设句子总是以点.结束)

poisci_pare("Hello world. This is great.")
>>>output: ["Hello world", "This is great"]

我需要编写什么来让python像这样分割字符串:

poisci_pare("Hello world. This is great.")
>>>output: [["Hello", "world"], ["This", "is", "great"]]

2 个答案:

答案 0 :(得分:3)

def poisci_pare(text):
    sents = text.split('.')
    answer = [sent.split() for sent in sents if sent]
    return answer

输出:

In [8]: poisci_pare("Hello world. This is great.")
Out[8]: [['Hello', 'world'], ['This', 'is', 'great']]

答案 1 :(得分:0)

这也可以解决问题:

input = "Hello world. This is great."
print [s.split() for s in input.split('.') if s.split()]
[['Hello', 'world'], ['This', 'is', 'great']]