加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby – 是找到两个非常大的数组之间差异的有效方法吗?

发布时间:2020-12-16 21:33:24 所属栏目:百科 来源:网络整理
导读:在找到两个非常大的数组之间的差异时,我遇到了关于效率和算法的问题.我希望对算法有很好理解的人可以指出我如何解决这个问题的正确方向,因为我目前的实现需要花费很长时间. 问题: 我有两个非常大的数组.一个包含具有无效域名的电子邮件列表,另一个是我需要
在找到两个非常大的数组之间的差异时,我遇到了关于效率和算法的问题.我希望对算法有很好理解的人可以指出我如何解决这个问题的正确方向,因为我目前的实现需要花费很长时间.

问题:

我有两个非常大的数组.一个包含具有无效域名的电子邮件列表,另一个是我需要针对第一个阵列检查的混合列表.

accounts_with_failed_email_domains = [279,000 records in here]

unchecked_account_domains = [149,000 records in here]

我需要做的是浏览unchecked_account_domains列表,然后比较每个条目以查看accounts_with_failed_email_domains中是否存在匹配项.我需要在列表之间插入所有匹配项,以便稍后处理.

如何有效地编写可以快速检查这些帐户的内容.这是我到目前为止所尝试的.

unchecked_account_domains = [really big array]
unchecked_account_domains = unchecked_account_domains.sort

accounts_with_failed_email_domains = [another huge array].sort

unchecked_account_domains.keep_if do |email|
  accounts_with_failed_email_domains.any? { |failed_email| email == failed_email }
end

# Count to see how many accounts are left
puts unchecked_account_domains.count

以上实现一直在运行.这是第二次尝试,但仍然证明没有更好.

unchecked_account_domains = [really big array]
unchecked_account_domains = unchecked_account_domains.sort

accounts_with_failed_email_domains = [another huge array].sort

unchecked_account_domains.each do |email|
  accounts_with_failed_email_domains.bsearch do |failed_email| 
     final_check << email if email == failed_email 
  end
end

# Count to see how many accounts are left
puts final_check.count

bsearch似乎很有希望,但我很确定我没有正确使用它.另外,我试着调查这个问题comparing large lists,但这是在python中,我似乎无法找到一个Ruby等效的集合.有没有人对如何解决这个问题有任何想法?

解决方法

看起来你可以使用Array# – :
result = unchecked_account_domains - accounts_with_failed_email_domains

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读