我有一个 txt 文件中的单词列表,以及一个列表中的数千个单词。我如何搜索它们并将它们添加到新列表中(如果找到)
words.txt 文件包含 100k 单词。 my_list 包含 4k 个单词。
目前我正在这样做。
my_list = [hello, hi, hey, ho, wow, .....]
with open("words.txt") as f:
lines = [line.rstrip() for line in f]
words_in_lines = []
for i in my_list:
if i in lines:
words_in_lines.append(i)
这是没有结束的,它不会执行,因为单词列表中的单词太多。
最佳答案
- 将
my_list
从列表转换为集合以加快查找速度 - 不要从
my_list
中逐行查找单词,而是在my_list
内的line
中搜索单词
my_list = set([hello, hi, hey, ho, wow, .....])
words_in_lines = []
with open("words.txt") as f:
for line in f:
words = line.strip().split()
for word in words:
if word in my_list:
words_in_lines.append(word)
时间复杂度应为O(文件中的单词数)
编辑:正如@greybeard 所指出的,这种方法
words_in_lines
中的单词顺序不同- 将
my_list
从列表修改为集合 - 跳过 10 万单词列表的创建
关于python - 在 100k len 的单词列表中查找 4k 个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71375382/