所以我有以下字符串:
string1 = " This is random text! I am trying to split this text into a nested list. Where each sentence is its own list and the values of that list contain the words and punctuation of that sentence. Is this possible? If not, why not? "
我需要将其拆分为嵌套列表,如下所示。
nested_list = [["This", "is", "random", "text", "!"] ["I", "am", "trying", "to", "split", "this", "text", "into", "a", "nested", "list", "."], ["Where", "each", "sentence", "is", "its", "own", "list", "and", "the", "values", "of", "that", "list", "contain", "the", "words", "and", "punctuation", "of", "that", "sentence", "."], ["Is", "this", "possible", "?"], ["If", "not", ",", "why", "not", "?"]]
我知道string.split()方法可以接受参数,但我无法弄清楚在所有时期,问号和感叹号上分段。因为根据我的理解,string.split()只接受一个参数。
答案 0 :(得分:1)
您可以使用内置的python:
import re
re.split(r'[!?.|,|\*|\n]',string1)