【问题标题】:Turn nested hash into single hash将嵌套散列变成单个散列
【发布时间】:2018-11-07 12:52:44
【问题描述】:

我要转这个示例数据:

{"2018-05-16"=>{
  "ABCD"=>{929119=>0.14174555, 338296=>0.13858019, 332058=>0.13680765, 449614=>0.13679536}}}

进入这个单一的哈希数组:

[
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>929119, "value"=>0.14174555},
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>338296, "value"=>0.13858019},
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>332058, "value"=>0.13680765},
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>449614, "value"=>0.13679536}
]

【问题讨论】:

  • 你有什么问题?
  • 这个问题是隐含的,我想对于这个论坛的聪明人来说不需要明确。如果没有,那我该如何实现上述目标?
  • sawa 的意思是:“你编写的代码试图解决这个问题的具体问题是什么”。就目前而言,您似乎希望我们为您编写代码,因此最好包含您编写的代码、您收到的错误消息,并解释如何将输入转换为输出而不是仅仅给出一个例子。
  • 明白。我已经解决了这个问题。感谢大家的帮助,代码或其他方式。

标签: arrays ruby hash nested


【解决方案1】:
h = { "2018-05-16"=>{
        "ABCD"=>{
          929119=>0.14174555,
          338296=>0.13858019,
          332058=>0.13680765,
          449614=>0.13679536
        }
      }
    }

dat, v = h.first
  #=> ["2018-05-16", {"ABCD"=>{929119=>0.14174555, 338296=>0.13858019,
                               332058=>0.13680765, 449614=>0.13679536}}]
loc, f = v.first
  #=> ["ABCD", {929119=>0.14174555, 338296=>0.13858019, 332058=>0.13680765,
  #             449614=>0.13679536}]
g = {"target_date"=>dat, "location"=>loc }
  #=> {"target_date"=>"2018-05-16", "location"=>"ABCD"}
f.map { |k,v| g.merge("id"=>k, "value"=>v) }
  #=> [{"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>929119,
  #     "value"=>0.14174555},
  #    {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>338296,
  #     "value"=>0.13858019},
  #    {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>332058,
  #     "value"=>0.13680765},
  #    {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>449614,
  #     "value"=>0.13679536}]

下面是另一种可以概括的方式。

h.each_with_object([]) do |(k0,v0),arr|
  v0.each do |k1,v1|
    v1.each.map do |k2,v2| 
      arr << { "target_date"=>k0, "location"=>k1, "id"=>k2, "value"=>v2 }
    end
  end
end
  #=> <same as for first method>

请注意,这适用于具有多个日期和/或位置的哈希,如下所示。

{ "2018-05-16"=>{ "ABCD"=>{ 92=>0.14, 44=>0.13 }, "EFGH"=>{ 12=>0.24, 34=>0.23 } },
  "2018-05-17"=>{ "ABCD"=>{ 52=>0.34, 34=>0.33 }, "EFGH"=>{ 42=>0.44, 74=>0.43 } } }

对于此哈希,将返回以下数组。

[{"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>92, "value"=>0.14},
 {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>44, "value"=>0.13},
 {"target_date"=>"2018-05-16", "location"=>"EFGH", "id"=>12, "value"=>0.24},
 {"target_date"=>"2018-05-16", "location"=>"EFGH", "id"=>34, "value"=>0.23},
 {"target_date"=>"2018-05-17", "location"=>"ABCD", "id"=>52, "value"=>0.34},
 {"target_date"=>"2018-05-17", "location"=>"ABCD", "id"=>34, "value"=>0.33},
 {"target_date"=>"2018-05-17", "location"=>"EFGH", "id"=>42, "value"=>0.44},
 {"target_date"=>"2018-05-17", "location"=>"EFGH", "id"=>74, "value"=>0.43}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-24
    • 2013-03-14
    • 2012-03-27
    • 1970-01-01
    • 2013-07-08
    • 2016-08-23
    • 2013-05-16
    • 2017-10-02
    相关资源
    最近更新 更多