博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
查找Python中字符串中所有单词出现的所有索引
阅读量:2529 次
发布时间:2019-05-11

本文共 2202 字,大约阅读时间需要 7 分钟。

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 module's (re) method finditer() which takes the 'word' or substring to be searched for and the sentence in which the 'word' shall be searched, as the argument.

以下示例说明了正则表达式模块的(re)方法finditer()的用法,该方法以要搜索的“单词”或子字符串以及应搜索“单词”的句子作为参数。

The output of the below example is the start index and end index of the word 'to' in the sentence.

以下示例的输出是句子中单词“ to”的开始索引和结束索引。

Code:

码:

# importing the moduleimport re# sentence and word to be foundsentence = "This is a sample sentence to search for include help to search all occurrences"word = "to"# logicfor match in re.finditer(word, sentence):    print("match found from {} to {}".format(match.start(), match.end()))

Output

输出量

match found from 26 to 28match found from 53 to 55

2)使用str.index() (2) Using the str.index())

Python:

Python:

def using_str_index (word, sentence):    index = 0;    while index < len(sentence):        index = sentence.find(word, index)        if index == -1:            break        print('{} found at {}'.format(word, index))        index += len(word)if __name__ == '__main__':        sentence = "This is a sample sentence to search for include help to search all occurrences"    word = "to"    using_str_index(word, sentence)

Output

输出量

to found at 26to found at 53

3)使用理解 (3) Using comprehension)

Code:

码:

def using_comprehension(word, sentence):    print([n for n in range(len(sentence)) if sentence.find(word, n) == n])if __name__== "__main__":    sentence = "This is a sample sentence to search for include help to search all occurrences"    word = "to"    using_comprehension(word, sentence)

Output

输出量

[26, 53]

翻译自:

转载地址:http://ikxzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>