我正在寻找一种在 Python 中检测字符串中每个单词第一个字符的索引的方法:
string = "DAY MONTH YEAR THIS HAS RANDOM SPACES"
我想找到每个“单词”的位置,所以这个输出将是:
[0, 4, 15, 23...]
有谁知道我如何才能做到这一点?
最佳答案
>>> import re
>>> string = "DAY MONTH YEAR THIS HAS RANDOM SPACES"
>>> [match.start() for match in re.finditer(r'\b\w', string)]
[0, 4, 15, 23, 30, 34, 43]
关于 python 2.7 : how to find the index of the first letter of each word in a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15969513/