ruby - 基于匹配键的Ruby组散列并将不匹配键的值存储在数组中
问题描述
需要根据下面的哈希值来实现给定的输出
foos = [ { :key => 'Foo', :value => 1, :revenue => 2 },
{ :key => 'Foo', :value => 1, :revenue => 4 },
{ :key => 'Bar', :value => 2, :revenue => 7 },
{ :key => 'bar', :value => 2, :revenue => 9 },
{ :key => 'Zampa', :value => 4, :revenue => 9 }]
输出应该是:
{ :key => 'Foo', :value => 1, :revenue => [2,4] } #Merging row 1 & 2 as they share same :key 'Foo'
{ :key => 'Bar', :value => 2, :revenue => [7,9] } #Merging row 3 & 4 as they share same :key 'Bar'
{ :key => 'Zampa', :value => 4, :revenue => 9 }
合并应该基于:Key字段的值如何在 ruby 中实现这一点,因为我是 ruby 新手。
解决方案
您可以使用group_by
对foos
数组进行分组:key
。但是,我会首先downcase
考虑:key
价值,因为您希望'Bar'
并'bar'
最终进入同一组。
# We first need to unify the keys of the hashes before we can start
# grouping. 'Bar' != 'bar' so they would be split up in two separate
# groups. Judging from the output you don't want this.
foos.each { |foo| foo[:key].downcase! }
# Now that all keys are downcased we can group based upon the value of
# the :key key.
grouped_foos = foos.group_by { |foo| foo[:key] }
# Now we need to map over the resulting hash and create a single result
# for each group.
grouped_foos.transform_values! do |foos|
# First I'll transform the structure of `foos`, from:
#
# [{a: 1, b: 2}, {a: 3, b: 4}]
#
# into:
#
# [[:a, 1], [:b, 2], [:a, 3], [:b, 4]]
#
tmp = foos.flat_map(&:to_a)
# Then I'll group the above structure based upon the first value in
# each array, simultaneously removing the first element. Resulting in:
#
# {a: [[1], [3]], b: [[2], [4]]}
#
tmp = tmp.group_by(&:shift)
# We now need to flatten the values by one level. Resulting in:
#
# {a: [1, 3], b: [2, 4]}
#
tmp.transform_values! { |values| values.flatten(1) }
# The next step is remove duplicate values. We currently have:
#
# {key: ['foo', 'foo'], value: [1, 1], revenue: [2, 4]}
#
# whereas we want:
#
# {key: ['foo'], value: [1], revenue: [2, 4]}
#
tmp.transform_values!(&:uniq)
# Lastly if the array only contains a single value we want to use the
# value instead of an array. Transforming the above structure into:
#
# {key: 'foo', value: 1, revenue: [2, 4]}
#
tmp.transform_values! { |head, *tail| tail.empty? ? head : [head, *tail] }
# Finally we need to return our new hash.
tmp
end
结合以上步骤,我们得到以下结果:
foos.each { |foo| foo[:key].downcase! }
grouped_foos = foos.group_by { |foo| foo[:key] }
grouped_foos.transform_values! do |foos|
foos.flat_map(&:to_a).group_by(&:shift)
.transform_values { |values| values.flatten(1).uniq }
.transform_values { |head, *tail| tail.empty? ? head : [head, *tail] }
end
如果您不想修改(大写的)原始foos
结构,则必须替换:
foos.each { |foo| foo[:key].downcase! }
# with
unified_keys = foos.map(&:dup).each { |foo| foo[:key] = foo[:key].downcase }
然后从那时起使用新unified_keys
结构。
上述解决方案产生以下结果:
grouped_foos
#=> {"foo" =>{:key=>"foo", :value=>1, :revenue=>[2, 4]},
# "bar" =>{:key=>"bar", :value=>2, :revenue=>[7, 9]},
# "zampa"=>{:key=>"zampa", :value=>4, :revenue=>9}}
您可以通过请求以下值来获得所需的结果grouped_foos
:
grouped_foos.values
#=> [{:key=>"foo", :value=>1, :revenue=>[2, 4]},
# {:key=>"bar", :value=>2, :revenue=>[7, 9]},
# {:key=>"zampa", :value=>4, :revenue=>9}]
推荐阅读
- osgi - 如何解决 Adobe Experience Manager 中包含组件期间的错误?
- spring - 具有子系统并可动态添加的自定义 Spring Actuator Endpoint
- r - 使用 bind_rows 将命名向量转换为 df
- typescript - Typescript 属性装饰器问题 - 在初始化之前无法访问
- angular - 如何让 Material Calender 的日期选择器过滤器方法与 Observables 一起使用
- npgsql - 其他所有查询都因语法错误而失败
- c++ - C++ 句子回文
- tensorflow - tensorflow如何填充像pytorch的'collate_fn'这样的批处理文本?
- google-api - 如何让 Google Home (Mini) 发布它收听的 MQTT 主题(和代理)?
- node.js - API 调用中所有函数的 1 db 连接