【问题标题】:Splitting sentences but still keeping the delimeters?拆分句子但仍保留分隔符?
【发布时间】:2021-06-07 10:19:52
【问题描述】:

AXIS 2/4 金额从 AXIS 活期账户转移到 TERM LOAN 账户活期账户

对于上面的例子,我需要这样的输出:

python(使用TO关键字分句的一些方法)

Output : [AXIS 2/4 amount transferred from AXIS current account] [to TERM LOAN account current account]

【问题讨论】:

    标签: python string split


    【解决方案1】:

    您可以使用 split() 函数拆分字符串,然后将“to”字符串附加到最后一个元素。

    这是一个sn-p:

    my_string = "AXIS 2/4 amount transferred from AXIS current account to TERM LOAN account current account"
    my_result = my_string.split("to")
    my_result[-1] = "to "+my_result[-1]
    

    【讨论】:

      【解决方案2】:

      不需要拆分,只要找到to的索引,取切片即可。

      text ="AXIS 2/4 amount transferred from AXIS current account to TERM LOAN account current account"
      
      [text[:text.find('to')], text[text.find('to'):]]
      
      #output 
      ['AXIS 2/4 amount transferred from AXIS current account ', 'to TERM LOAN account current account']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-14
        • 2018-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 1970-01-01
        相关资源
        最近更新 更多