2015-02-06 171 views
0

我有数组:红宝石分组元素

a = [1, 3, 1, 3, 2, 1, 2] 

我想GROUP BY值,但将其保存索引,所以结果一定是这个样子的:

[[0, 2, 5], [1, 3], [4, 6]] 

或哈希

{1=>[0, 2, 5], 3=>[1, 3], 2=>[4, 6]} 

现在我正在使用相当难看又大的代码:

struc = Struct.new(:index, :value) 
array = array.map.with_index{ |v, i| struc.new(i, v) }.group_by {|s| s[1]}.map { |h| h[1].map { |e| e[0]}} 

`

回答

2

如果你使用一个哈希默认值以避免在元素上迭代两次:

a = [1, 3, 1, 3, 2, 1, 2] 

Hash.new { |h, k| h[k] = [] }.tap do |result| 
    a.each_with_index { |i, n| result[i] << n } 
end 
#=> { 1 => [0, 2, 5], 3 => [1, 3], 2 => [4, 6] } 
+1

好,或'a.each_with_index.with_object(Hash.new {| h,k | h [k] = []}){|(e,i),h | h [e] << i}'。 – 2015-02-06 23:04:58

+0

谢谢@CarySwoveland:TIL'with_object',从未考虑过此方法。 – spickermann 2015-02-06 23:08:57

+0

你也可以使用[Enumerator#with_index](http://ruby-doc.org/core-2.1.5/Enumerator.html#method-i-with_index):'a.each_with_object(Hash.new {| h, k | h [k] = []})。with_index {|(e,h),i | h [e] << i}'。请注意块变量的不同顺序。 TIL TIL。 – 2015-02-06 23:48:27

2
a = [1, 3, 1, 3, 2, 1, 2] 
a.each_with_index.group_by(&:first).values.map { |h| h.map &:last } 

首先,我们在形式[val, idx], ...each_with_index)得到一个Enumerator,然后group_by的值(在对first值),则取每对的索引(last元素)。