我有一个短字符串的长列表,并且我想要在通常长度为~10,000个字符的文本中搜索所有这些项目。我的列表有大约500个短字符串,我希望使用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中最快的方法吗?虽然不算慢,但在大规模操作时(例如搜索列表中有500个项目,每个项目的cleanText都有10,000个字符),速度似乎比我想象的要慢一些。