首页 > 解决方案 > 拆分句子但仍保留分隔符?

问题描述

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

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

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

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

标签: pythonstringsplit

解决方案


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

这是一个片段:

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]

推荐阅读