酷代码 AI
菜单
服务商

Python 实现:轻松查询文档中指定内容的所有索引

以下是一个使用 Python 实现的简单文档内容查询程序示例,它可以查找文档中指定字符串的索引: ```python def search_document(document, target): indices = [] start = 0 while True: index = document.find(target, start) if index == -1: break indices.append(index) start = index + 1 return indices document = "it is not the critic who counts, not the man who points out how the strong man stumbles, the doer of deeds could have done them better. the credit belongs to the man who is actually in the are 'ns, whose face is marred by dust and sweat and blood; who strives valiantly; who errs, and comes short again and again ;because there is not effort without error and shortcoming; but who does actually strive to do the deeds; who knows the great enthusiasms, the grea" target = "man" print(search_document(document, target)) ``` 在上述示例中,定义了一个 `search_document` 函数,它接受文档内容和要查找的目标字符串作为参数,并返回目标字符串在文档中的所有索引位置。 ######[AI写代码神器 | 259点数解答 | 2024-10-22 15:41:34]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]