我有大约 5000 个文件,我需要从 10000 个单词的列表中找到每个文件中的单词。我当前的代码使用(非常)长的正则表达式来执行此操作,但速度非常慢。
wordlist = [...list of around 10000 english words...]
filelist = [...list of around 5000 filenames...]
wordlistre = re.compile('|'.join(wordlist), re.IGNORECASE)
discovered = []
for x in filelist:
with open(x, 'r') as f:
found = wordlistre.findall(f.read())
if found:
discovered = [x, found]
这以每秒大约 5 个文件的速度检查文件,这比手动执行要快得多,但仍然非常慢。有一个更好的方法吗?