查找保持Ruby的哈希值重复 [英] Find keep duplicates in Ruby hashes
问题描述
我有哪里我需要找到基于散列之间的一个匹配的散列值和存储匹配的数组。
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屋!