【问题标题】:How do I split sentence on 'and'?如何在“和”上拆分句子?
【发布时间】:2021-10-01 19:53:55
【问题描述】:

我试图将我的句子拆分为“和”,但有些结果看起来像这样

我的代码

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']

【问题讨论】:

标签: python-3.x


【解决方案1】:

尝试做

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    相关资源
    最近更新 更多