我有一个很长的短字符串列表,我想在(通常)很长的文本字符串中搜索所有这些项目。我的列表长度约为500个短字符串,我希望能够使用python查找出现在大约〜10,000个字符的源文本中的所有内容。在python中搜索文本正文中的多个项目的最快方法
这里是我的问题的一个简单的例子:
cleanText = "four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal"
searchList = ["years ago","dedicated to","civil war","brought forth"]
我目前在cleanText发现在searchList出现的项目的方法是:
found = [phrase for phrase in searchList if phrase in cleanText]
这是在Python的最快方法是什么?它并不是很慢,但在规模上(searchList中有500个项目,其中有一个长度为10,000个字符的cleanText),它看起来比我想要的慢一点。
您的内容是否持续存在?你可以使用全文索引解决方案吗? –