Skip to main content
 首页 » 编程设计

python之在每三个空格上拆分一个字符串

2025年05月04日21mate10pro

如何在每三个空格上拆分一个字符串?我想知道 python 是否有内置语法,或者是否可以使用列表理解来完成。

"a bb c dd ee f" -> ["a bb c", "dd ee f"] 

请您参考如下方法:

re.split(r'(.*?\s.*?\s.*?)\s', "a bb c dd ee f") 

为了从结果中删除空字符串:

[x for x in re.split(r'(.*?\s.*?\s.*?)\s', "a bb c dd ee f") if x]