[英]Group by array element within a hash, calculate counts
I have a Ruby hash: 我有一个Ruby哈希:
example = {
:key1 => [1, 1, 4],
:key2 => [1, 2, 3],
:key3 => [1, 3, 2],
:key4 => [1, 5, 0],
:key5 => [1, 7, 2],
:key6 => [2, 1, 5],
:key7 => [2, 2, 4],
:key8 => [2, 4, 2],
:key9 => [3, 1, 6],
:key10 => [3, 2, 5],
:key11 => [3, 3, 4]
}
How can I group the hash by the first element in the value's array? 如何按值数组中的第一个元素对哈希进行分组? Once it is grouped, how can I get count of each of those groups and store them into an additional hash?
将其分组后,如何获得每个组的计数并将其存储到其他哈希中?
I'm open to skipping the group_by
part if I'm able to extract the counts. 如果我能够提取计数,则可以跳过
group_by
部分。
Example desired output: 所需输出示例:
groups = {:group1 => 5, :group2 => 3, :group3 => 3}
Here is a way using each_with_object
: 这是使用
each_with_object
的方法:
example.each_with_object(Hash.new(0)) { |(_, (v, *)), h| h[:"group#{v}"] += 1 }
# => {:group1=>5, :group2=>3, :group3=>3}
这是“简易”版本的一个(不带键的数组):
example.group_by { |k, v| v.first }.values.map(&:count)
This is one way to do that: 这是一种方法:
example.values.group_by(&:first).each_with_object({}) { |(k,v),h|
h.update("group#{k}".to_sym=>v.size) }
#=> {:group1=>5, :group2=>3, :group3=>3}
The steps: 步骤:
example = {:key1=> [1, 1, 4], :key2=> [1, 2, 3], :key3=>[1, 3, 2],
:key4=> [1, 5, 0], :key5=> [1, 7, 2], :key6=>[2, 1, 5],
:key7=> [2, 2, 4], :key8=> [2, 4, 2], :key9=>[3, 1, 6],
:key10=>[3, 2, 5], :key11=>[3, 3, 4]}
v = example.values
#=> [[1, 1, 4], [1, 2, 3], [1, 3, 2], [1, 5, 0], [1, 7, 2], [2, 1, 5],
# [2, 2, 4], [2, 4, 2], [3, 1, 6], [3, 2, 5], [3, 3, 4]]
g = v.group_by(&:first) # same as group_by { |k,_| k }
#=> {1=>[[1, 1, 4], [1, 2, 3], [1, 3, 2], [1, 5, 0], [1, 7, 2]],
# 2=>[[2, 1, 5], [2, 2, 4], [2, 4, 2]],
# 3=>[[3, 1, 6], [3, 2, 5], [3, 3, 4]]}
enum = g.each_with_object({})
#=> #<Enumerator: { 1=>[[1, 1, 4], [1, 2, 3], [1, 3, 2], [1, 5, 0], [1, 7, 2]],
# 2=>[[2, 1, 5], [2, 2, 4], [2, 4, 2]],
# 3=>[[3, 1, 6], [3, 2, 5], [3, 3, 4]]
# }:each_with_object({})>
Convert the enumerator to an array to examine its values: 将枚举器转换为数组以检查其值:
enum.to_a
#=> [[[1, [[1, 1, 4], [1, 2, 3], [1, 3, 2], [1, 5, 0], [1, 7, 2]]], {}],
# [[2, [[2, 1, 5], [2, 2, 4], [2, 4, 2]]], {}],
# [[3, [[3, 1, 6], [3, 2, 5], [3, 3, 4]]], {}]]
Note the value of the hash, which presently is empty. 注意哈希值,目前为空。 Pass the first element of
enum
to the block: 将
enum
的第一个元素传递到块:
(k,v),h = enum.next
#=> [[1, [[1, 1, 4], [1, 2, 3], [1, 3, 2], [1, 5, 0], [1, 7, 2]]], {}]
k #=> 1
v #=> [[1, 1, 4], [1, 2, 3], [1, 3, 2], [1, 5, 0], [1, 7, 2]]
h #=> {}
h.update("group#{k}".to_sym=>v.size)
#=> {}.update(:group1=>5)
#=> {:group1=>5} (new value of h)
If we now examine the elements of enum
, we see the value of the hash has been updated: 如果现在检查
enum
的元素,我们将看到哈希值已更新:
enum.to_a
#=> [[[1, [[1, 1, 4], [1, 2, 3], [1, 3, 2], [1, 5, 0], [1, 7, 2]]],
# {:group1=>5}],
# [[2, [[2, 1, 5], [2, 2, 4], [2, 4, 2]]], {:group1=>5}],
# [[3, [[3, 1, 6], [3, 2, 5], [3, 3, 4]]], {:group1=>5}]]
Continuing... 继续...
(k,v),h = enum.next
#=> [[2, [[2, 1, 5], [2, 2, 4], [2, 4, 2]]], {:group1=>5}]
k #=> 2
v #=> [[2, 1, 5], [2, 2, 4], [2, 4, 2]]
h #=> {:group1=>5}
h.update("group#{k}".to_sym=>v.size)
#=> {:group1=>5, :group2=>3}
(k,v),h = enum.next
#=> [[3, [[3, 1, 6], [3, 2, 5], [3, 3, 4]]], {:group1=>5, :group2=>3}]
h.update("group#{k}".to_sym=>v.size)
#=> {:group1=>5, :group2=>3, :group3=>3}
Or it might be clearer like this: 或者可能更像这样:
arrays_by_first_element = example.values.group_by { |a| a[0] }
groups = {}
arrays_by_first_element.each { |k, v| groups[k] = v.size }
Simple one liner: 简单的一个班轮:
puts example.map{|k,v| v[0]}.inject(Hash.new(0)) { |total, e| total[("group%s" % e)] += 1; total}
output: 输出:
{"group1"=>5, "group2"=>3, "group3"=>3}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.