我有一個很長的短字符串列表,我想在(通常)很長的文本字符串中搜索所有這些項目。我的列表長度約爲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),它看起來比我想要的慢一點。
您的內容是否持續存在?你可以使用全文索引解決方案嗎? –