【发布时间】:2012-09-05 17:41:01
【问题描述】:
我有一长串短字符串,我想在(通常)长文本字符串中搜索所有这些项目。我的列表的长度约为 500 个短字符串,我想使用 python 查找大约 10,000 个字符长的源文本中出现的所有内容。
这是我的问题的一个简短示例:
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 个项目,cleanText 的长度为 10,000 个字符)似乎比我想要的要慢一些。
【问题讨论】:
-
您的内容是否具有持久性?您可以使用全文索引解决方案吗?