酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

用python完成一个简单的文档内容查询程序,要求能够查出文档中所有需要的内容的索引。 文档: 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,

Here is a Python code that can be used to create a simple document content search program: ```python def search_document(document, keyword): index = [] # Split the document into individual words words = document.lower().split() # Iterate over each word and check if it contains the keyword for i, word in enumerate(words): if keyword.lower() in word: index.append(i) return index # Test the search function with the given document 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," keyword = "the" result = search_document(document, keyword) if len(result) > 0: print(f"The keyword '{keyword}' was found at index positions: {', '.join(map(str, result))}") else: print(f"The keyword '{keyword}' was not found in the document.") ``` This code defines a function `search_document` that takes a document and a keyword as input. It splits the document into individual words, converts them to lowercase for case-insensitive matching, and checks each word if it contains the keyword. The function returns a list of indices where the keyword was found. In the example, the function is tested with the given document and the keyword "the". If the keyword is found in the document, the function prints the indices where it was found. Otherwise, it prints a message that the keyword was not found. [2024-10-21 21:45:05 | AI写代码神器 | 1189点数解答]

相关提问