如何从数组创建哈希

时间:2015-02-12 05:59:32

标签: ruby arrays hash

这是我的阵列:

sorted_array = [["Friday", "42", 8], ["Friday", "34", 8], ["Friday", "41", 78], ["Friday", "35", 7], ["Friday", "40", 7], ["Friday", "36", 6], ["Friday", "39", 7], ["Friday", "37", 56], ["Friday", "38", 6], ["Monday", "38", 3], ["Monday", "39", 5], ["Monday", "37", 54], ["Monday", "40", 6], ["Monday", "36", 6], ["Monday", "41", 7], ["Monday", "35", 7], ["Monday", "42", 7], ["Monday", "34", 8]]

我需要做:

{ 'Friday' => [ ["42", 8], ["34", 8], ["41", 78], ["35", 7], [ "40", 7], ["36", 6], [ "39", 7], ["37", 56], [ "38", 6] ]  }

"Monday"相同。有人可以对此有所了解吗?

这是我从POST获取数据的方式:

{"0"=>{"Monday"=>{"34"=>8}, "Friday"=>{"34"=>8}}, "1"=>{"Monday"=>{"35"=>7}, "Friday"=>{"35"=>7}}, "2"=>{"Monday"=>{"36"=>6}, "Friday"=>{"36"=>6}}, "3"=>{"Monday"=>{"37"=>54}, "Friday"=>{"37"=>56}}, "4"=>{"Monday"=>{"38"=>3}, "Friday"=>{"38"=>6}}, "5"=>{"Monday"=>{"39"=>5}, "Friday"=>{"39"=>7}}, "6"=>{"Monday"=>{"40"=>6}, "Friday"=>{"40"=>7}}, "7"=>{"Monday"=>{"41"=>7}, "Friday"=>{"41"=>78}}, "8"=>{"Monday"=>{"42"=>7}, "Friday"=>{"42"=>8}}}

然后我把它变成数组并排序:

 hash_array = []
 specific_ranges.each do |key, value|
   value.each do |day, scores|
      scores.each do |a_score, amount|
         hash_array.push [day, a_score, amount]
      end
   end
 end

排序:

sorted_array = hash_array.sort_by &:first

7 个答案:

答案 0 :(得分:5)

Ruby MAGIC:

p sorted_array.group_by(&:shift) #=> {"Friday"=>[["42", 8], ["34", 8], ["41", 78], ["35", 7] ...

说明:shift获取数组的第一个值,同时将其从数组中删除,因此group_by函数可以准确地生成您要查找的内容。

答案 1 :(得分:2)

你可以直接尝试这样的数据:

data = {"0"=>{"Monday"=>{"34"=>8}, "Friday"=>{"34"=>8}}, "1"=>{"Monday"=>{"35"=>7}, "Friday"=>{"35"=>7}}, "2"=>{"Monday"=>{"36"=>6}, "Friday"=>{"36"=>6}}, "3"=>{"Monday"=>{"37"=>54}, "Friday"=>{"37"=>56}}, "4"=>{"Monday"=>{"38"=>3}, "Friday"=>{"38"=>6}}, "5"=>{"Monday"=>{"39"=>5}, "Friday"=>{"39"=>7}}, "6"=>{"Monday"=>{"40"=>6}, "Friday"=>{"40"=>7}}, "7"=>{"Monday"=>{"41"=>7}, "Friday"=>{"41"=>78}}, "8"=>{"Monday"=>{"42"=>7}, "Friday"=>{"42"=>8}}}

a ||= Hash.new { |h, k| h[k] = [] }   
data.each{ |d|
  d[1].keys.each { |key|
    a[key] << d[1][key]
  }
}

a
# => {"Monday"=>[{"34"=>8}, {"35"=>7}, {"36"=>6}, {"37"=>54}, {"38"=>3}, {"39"=>5}, {"40"=>6}, {"41"=>7}, {"42"=>7}], "Friday"=>[{"34"=>8}, {"35"=>7}, {"36"=>6}, {"37"=>56}, {"38"=>6}, {"39"=>7}, {"40"=>7}, {"41"=>78}, {"42"=>8}]}

如果需要,可以使用Array#uniq对哈希数组进行更深入的改进。

答案 2 :(得分:1)

 sorted_array.group_by(&:first).map { |k, v| [k, v.map { |a| a[1..-1] }] }.to_h
 # {"Friday"=>[["42", 8], ["34", 8], ["41", 78], ["35", 7], ["40", 7], ["36", 6], ["39", 7], ["37", 56], ["38", 6]],
 #  "Monday"=>[["38", 3], ["39", 5], ["37", 54], ["40", 6], ["36", 6], ["41", 7], ["35", 7], ["42", 7], ["34", 8]]} 

答案 3 :(得分:1)

h = sorted_array.group_by(&:first).each_value{|a| a.each(&:shift)}

h.select{|k, _| k == "Friday"}
#=> { 'Friday' => [ ["42", 8], ["34", 8], ["41", 78], ["35", 7], [ "40", 7], ["36", 6], [ "39", 7], ["37", 56], [ "38", 6] ]  }

答案 4 :(得分:0)

我有一个快速的解决方案。这可能不是最优的,但在这里:

sorted_array = [["Friday", "42", 8], ["Friday", "34", 8], ["Friday", "41", 78], ["Friday", "35", 7], ["Friday", "40", 7], ["Friday", "36", 6], ["Friday", "39", 7], ["Friday", "37", 56], ["Friday", "38", 6], ["Monday", "38", 3], ["Monday", "39", 5], ["Monday", "37", 54], ["Monday", "40", 6], ["Monday", "36", 6], ["Monday", "41", 7], ["Monday", "35", 7], ["Monday", "42", 7], ["Monday", "34", 8]]

s = sorted_array.select{|x| x[0] == 'Friday'}.each{|a| a.shift}

Hash['Friday', s]

这将根据您的需要返回结果。但在这样做时,我发现了代码行的限制:

s = sorted_array.select{|x| x[0] == 'Friday'}.each{|a| a.shift}

只运行一次,如果连续第二次运行,它将返回一个空白数组。所以,如果你使用它,请注意这一点。希望这会有所帮助。

答案 5 :(得分:0)

这是Array#reduce的标准任务。

sorted_array.reduce({}) { |memo, e| 
  (memo[e.first] ||= []) << e[1..-1]
  memo 
}

这里我们使用第一个元素作为键来完成每个数组元素的其余部分的哈希。

UPD 实际上,您不需要重新组织哈希,一切都可以在一步中完成:

h = {"0"=>{"Monday"=>{"34"=>8}, "Friday"=>{"34"=>8}}, "1"=>{"Monday"=>{"35"=>7}, "Friday"=>{"35"=>7}}, "2"=>{"Monday"=>{"36"=>6}, "Friday"=>{"36"=>6}}, "3"=>{"Monday"=>{"37"=>54}, "Friday"=>{"37"=>56}}, "4"=>{"Monday"=>{"38"=>3}, "Friday"=>{"38"=>6}}, "5"=>{"Monday"=>{"39"=>5}, "Friday"=>{"39"=>7}}, "6"=>{"Monday"=>{"40"=>6}, "Friday"=>{"40"=>7}}, "7"=>{"Monday"=>{"41"=>7}, "Friday"=>{"41"=>78}}, "8"=>{"Monday"=>{"42"=>7}, "Friday"=>{"42"=>8}}}
h.reduce({}) { |memo, k| 
  k.last.each { |k,v| (memo[k] ||= []) << v.to_a.flatten }
  memo 
}

答案 6 :(得分:0)

两个代价:

<强>#1

arr.each_with_object({}) { |a,h| h.update(a.shift=>[a]) { |_,o,n| o+n } }

<强>#2

h = arr.group_by(&:first)
h.each { |k,v| h[k] = v.transpose[1..-1].transpose }