查找Python中字符串中所有单词出现的所有索引

您所在的位置:网站首页 python输出字符索引值 查找Python中字符串中所有单词出现的所有索引

查找Python中字符串中所有单词出现的所有索引

2023-07-28 13:16| 来源: 网络整理| 查看: 265

To retrieve the index of all the occurrences of a word in the string (say a statement), Python, like most of the programming languages supports the regular expression module as a built-in module. Or if we don't want the overhead of using the regular expressions, we could also use the str.index() in an iteration to get the index and also using the comprehension.

为了检索字符串中某个单词所有出现的索引 (例如一条语句),Python与大多数编程语言一样,将正则表达式模块作为内置模块来支持。 或者,如果我们不想使用正则表达式的开销,我们也可以在迭代中使用str.index()来获取索引,也可以使用comprehension

1)使用正则表达式模块 (1) Using the regular expression module)

Example:

例:

The below example, explains the usage of the regex modu



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3