我有一个名为 tekst
的很长字符串(从文件中读取 600 MB)和一个名为 nlwoorden
的包含 11.000 个单词的列表。我想要拥有 tekst
中的所有内容,但不拥有 nlwoorden
中的所有内容。
belangrijk=[woord for woord in tekst.split() if woord not in nlwoorden]
会产生我想要的结果。显然,这需要很长时间来计算。有没有更有效的方法?
谢谢!
最佳答案
使用基于集合的解决方案可为您提供O(len(nlwoorden))
for the whole thing 。它应该需要另一个O(len(nlwoodden)) + O(len(tekst))
to make the two sets .
因此,您要查找的代码段基本上就是评论中列出的代码段:
belangrijk=list(set(tekst.split()) - set(nlwoorden))
(假设您希望最后再次将其作为列表)
关于python - 从字符串中获取不在另一个列表中的单词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24439832/