红宝石分组元素
问题描述:
我有数组:红宝石分组元素
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]}}
`
答
如果你使用一个哈希默认值以避免在元素上迭代两次:
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] }
答
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
元素)。
答
您可以使用:
a = [1, 3, 1, 3, 2, 1, 2]
a.each_with_index.group_by(&:first).values.map { |b| b.transpose.last }
#=> [[0, 2, 5], [1, 3], [4, 6]]
好,或'a.each_with_index.with_object(Hash.new {| h,k | h [k] = []}){|(e,i),h | h [e] 2015-02-06 23:04:58
谢谢@CarySwoveland:TIL'with_object',从未考虑过此方法。 – spickermann 2015-02-06 23:08:57
你也可以使用[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] 2015-02-06 23:48:27