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

前端之家收集整理的这篇文章主要介绍了Ruby – 是找到两个非常大的数组之间差异的有效方法吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在找到两个非常大的数组之间的差异时,我遇到了关于效率和算法的问题.我希望对算法有很好理解的人可以指出我如何解决这个问题的正确方向,因为我目前的实现需要花费很长时间.

问题:

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

  1. accounts_with_Failed_email_domains = [279,000 records in here]
  2.  
  3. unchecked_account_domains = [149,000 records in here]

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

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

  1. unchecked_account_domains = [really big array]
  2. unchecked_account_domains = unchecked_account_domains.sort
  3.  
  4. accounts_with_Failed_email_domains = [another huge array].sort
  5.  
  6. unchecked_account_domains.keep_if do |email|
  7. accounts_with_Failed_email_domains.any? { |Failed_email| email == Failed_email }
  8. end
  9.  
  10. # Count to see how many accounts are left
  11. puts unchecked_account_domains.count

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

  1. unchecked_account_domains = [really big array]
  2. unchecked_account_domains = unchecked_account_domains.sort
  3.  
  4. accounts_with_Failed_email_domains = [another huge array].sort
  5.  
  6. unchecked_account_domains.each do |email|
  7. accounts_with_Failed_email_domains.bsearch do |Failed_email|
  8. final_check << email if email == Failed_email
  9. end
  10. end
  11.  
  12. # Count to see how many accounts are left
  13. puts final_check.count

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

解决方法

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

猜你在找的Ruby相关文章