Skip to main content
 首页 » 编程设计

python之每个单词之间的空格数

2025年05月04日18yyy_WW

如何找到一种快速计算文本中每个单词之间的间距数的方法?

每个空格代表一个值,

示例:一个空格是字母“a”,两个空格是字母“b”等。

文本示例

文本:

hello all  the   world  

hello 和 all 之间有一个空格 --> 'a',all 和 the 之间有两个空格 --> 'b', ...

单词 --> 'abc'

请您参考如下方法:

import re 
import string 
 
''.join(map(lambda x: string.lowercase[len(x) - 1], re.findall(r'\s+', 'hello all  the   world'))) 
# 'abc'