如何在"和"上拆分句子?

  • 本文关键字:拆分 句子 python-3.x
  • 更新时间 :
  • 英文 :


我试图将我的句子分割为'和',但一些结果看起来像这样

我的代码
string = 'I am handling it because it is difficult and confusing'
string.split('and')

结果

['I am h', 'ling it because it is difficult ', ' confusing']

我正在试着得到这个。我该怎么做呢?

['I am handling it because it is difficult ', ' confusing']

试试

string.split(" and ")

它只挑选单词。但是如果你需要空格,这个函数/循环将做(测试):

add_spaces(x): 
x[0] += ' '
for i in range(1, len(x) - 1):
x[i] = ' ' + x[i]
x[i] += ' '
x[-1] = ' ' +  x[-1]

最新更新