python - 将单词列表与句子列表进行比较并打印匹配行的 Pythonic 方法
问题描述
我目前正在清理我们的数据库,它变得非常耗时。典型的
for email in emails:
循环在任何地方都不够快。
例如,我目前正在将 230,000 封电子邮件列表与 39,000,000 行完整记录列表进行比较。将这些电子邮件与它们所属的记录行匹配并打印需要几个小时。有谁知道如何在此查询中实现线程以加快速度?虽然这是超级快
strings = ("string1", "string2", "string3")
for line in file:
if any(s in line for s in strings):
print "yay!"
那永远不会打印匹配的线,只会打印针。
先感谢您
解决方案
一种可能性是使用 aset
来存储电子邮件。这使得检查if word in emails
O(1)。因此,完成的工作与文件中的总字数成正比:
emails = {"string1", "string2", "string3"} # this is a set
for line in f:
if any(word in emails for word in line.split()):
print("yay!")
您最初的解决方案是O(nm)(对于n 个单词和m个电子邮件),而不是O(n)与set
.
推荐阅读
- java - 一个 RecyclerView 项目中的 OnClick 会影响其他项目
- python - 我正在尝试安装“pip install mysqlclient”,但每次都失败。尝试了所有其他的东西,但也没有奏效。我该怎么办?
- javascript - NodeJS socket.io 在收到握手响应之前抛出错误连接关闭
- angular - 如何自动更新页面上的数据,Angular 7 和 Spring Boot
- javascript - 为什么以及何时使用 Promise.resolve?
- java - 特定插入的事务回滚
- laravel - Laravel 迁移错误号:150“外键约束格式不正确”
- c - 如何读取多项式并将它们存储在一个数组中并在 C 中进行错误检查?
- c# - 用多行填充 RichTextbox 会挂起 UI?
- c# - KendoUi Grid 没有点击更新功能