gpt4 book ai didi

python 2.7 : how to find the index of the first letter of each word in a string

转载 作者:行者123 更新时间:2023-11-28 20:25:11 25 4
gpt4 key购买 nike

我正在寻找一种在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com