用于单词拆分
value = "HELLO HOW ARE YOU WHAT ARE YOU DOING"
result = value.split(" ",2)
>> output will be:['HELLO','HOW', 'ARE YOU WHAT ARE YOU DOING']
但是如果我想要这样的结果
output: ['HELLO HOW ARE YOU WHAT ARE' ,'YOU' ,'DOING']
答案 0 :(得分:3)
使用rsplit
value = "HELLO HOW ARE YOU WHAT ARE YOU DOING"
result = value.rsplit(" ", 2)
print(result) # ['HELLO HOW ARE YOU WHAT ARE', 'YOU', 'DOING']
答案 1 :(得分:1)
只需使用rsplit
代替split
。
In[7]: s.rsplit(" ", 2)
Out[7]: ['HELLO HOW ARE YOU WHAT ARE', 'YOU', 'DOING']