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

标签 python python-2.7

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

相关文章:

python - 如何在不更改字节的情况下将字符串转换为 float ?

python - 使用 Spyder/Python 打开 .npy 文件

python - 在 Maya 2013 中使用 lxml?

python - 在python中查找一个文件中不在另一个文件中的所有数字

python - 如何在 PyQt4 中杀死单发 QtCore.QTimer?

python - 如何确保 re.findall() 停在正确的位置?

python - 将 python 3.x 移植到 2.7 时 to_bytes 中的问题

python - _tkinter.Tcl错误: couldn't recognize data in image file "background.png" when using turtle graphics

python - 在给定两个列表的情况下查找前 K 个产品的有效方法

python - Django-rest框架结构不捕获post或put请求