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

在找到两个非常大的数组之间的差异时,我遇到了关于效率和算法的问题.我希望对算法有很好理解的人可以指出我如何解决这个问题的正确方向,因为我目前的实现需要花费很长时间.

问题:

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

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...