我目前正在尝试根据特定单词拆分字符串。 我想要实现的一个例子是
string =" Total number of boys is 2020 , Total number of states could be 19? Total number of votes is 400"
我希望只要遇到Total这个词,就会拆分它。 我希望拆分的结果具有以下模式
results=['Total number of boys is 2020 ,' , 'Total number of states could be 19? ', 'Total number of votes is 400']
答案 0 :(得分:2)
以下将找到以“总计”开头并以标点字符.
,,
或?
结尾的句子。你没有提到要求用标点符号限制提取的字符串,但我怀疑你会发现它很方便。
>>> [m[0] + m[2] for m in re.findall('(Total(.*?))([,?.]|$)', string)]
['Total number of boys is 2020 ,', 'Total number of states could be 19?', 'Total number of votes is 400']
答案 1 :(得分:1)
def word_splitter(string, word):
my_list = []
for phrase in string.split(word):
if len(phrase.strip()) > 0:
my_list.append('%s%s' % (word, phrase))
return my_list
所以
string =" Total number of boys is 2020 , Total number of states could be 19? Total number of votes is 400"
word_splitter(string, 'Total ')
返回
['Total number of boys is 2020 , ', 'Total number of states could be 19? ', 'Total number of votes is 400']
答案 2 :(得分:1)
另一种解决方案:
re.findall('(?:Total|^).*?(?=(?:Total)|$)', string)
结果:
[' ', 'Total number of boys is 2020 , ', 'Total number of states could be 19? ', 'Total number of votes is 400']
答案 3 :(得分:0)
以下可以根据需要拆分该行。 首先,我们将字符串“string”拆分,然后将其添加到“delimiter”
['Total' + item for index, item in enumerate(string.split('Total')) if index!=0 and item]
结果:
['Total number of boys is 2020 , ', 'Total number of states could be 19? ', 'Total number of votes is 400']