python - 从字符串中获取不在另一个列表中的单词列表

标签 python python-2.7

我有一个名为 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/

相关文章:

python - 为什么 C 和 Python 中的递归 tetration 比迭代 tetration 更快?

python - 从python中的列表列表中删除逗号

python - 谷歌应用引擎: ImportError: No module named pkg_resources

python - TypeError : expected httplib. 消息,得到 <type 'instance' >。在 GAE 上使用 requests.get(url) 时

python - 合并几个 Python 字典

python - django-oscar 中的用户货到付款

python3编码二进制时的问题

Python:元组中的列表

python - 列表理解产生结果加上乱码

python - 我怎样才能取回一个类的动态创建的对象?