查找保持Ruby的哈希值重复 [英] Find keep duplicates in Ruby hashes

查看:165
本文介绍了查找保持Ruby的哈希值重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪里我需要找到基于散列之间的一个匹配的散列值和存储匹配的数组。

I have an array of hashes where I need to find and store matches based on one matching value between the hashes.

a = [{:id => 1, :name => "Jim", :email => "jim@jim.jim"}, 
     {:id => 2, :name => "Paul", :email => "paul@paul.paul"}, 
     {:id => 3, :name => "Tom", :email => "tom@tom.tom"}, 
     {:id => 1, :name => "Jim", :email => "jim@jim.jim"}, 
     {:id => 5, :name => "Tom", :email => "tom@tom.tom"}, 
     {:id => 6, :name => "Jim", :email => "jim@jim.jim"}]

所以,我希望回到

So I would want to return

b = [{:id => 1, :name => "Jim", :email => "jim@jim.jim"},  
     {:id => 3, :name => "Tom", :email => "tom@tom.tom"}, 
     {:id => 5, :name => "Tom", :email => "tom@tom.tom"}, 
     {:id => 6, :name => "Jim", :email => "jim@jim.jim"}]

注:所以他们没有很好地被分组,只是不准确名称事实后:我可以把数据(CSV)由排序。此外,它没有必要两个相同的,也可能是3或10个或更多。

Notes: I can sort the data (csv) by :name after the fact so they don't have to be nicely grouped, just accurate. Also it's not necessary two of the same, it could be 3 or 10 or more.

另外,数据是约22000的行

Also, the data is about 22,000 rows.

推荐答案

我测试这一点,它会做你想要什么:

I tested this and it will do exactly what you want:

b = a.group_by { |h| h[:name] }.values.select { |a| a.size > 1 }.flatten

不过,你可能想看看一些在计算产生的中间对象,看看如果这些对你更有用。

However, you might want to look at some of the intermediate objects produced in that calculation and see if those are more useful to you.

这篇关于查找保持Ruby的哈希值重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆