将具有共同值/键的数组转换为散列

时间:2021-05-29 16:46:09

I have an array of arrays like so:

我有一个数组,如下所示:

[[1,"A"], [1,"B"], [2,"C"], [2,"D"]]

I'd like to get the results into a Hash like so:

我想把结果散列成这样:

1 => {results => ["A","B"]}, 2 => {results => ["C","D"]}

I tried using the "group_by" methods and wasn't able to get it into this form. What's the most efficient way to do this?

我尝试使用“group_by”方法,但无法将其转换成这种形式。最有效的方法是什么?

Any ideas?

什么好主意吗?

4 个解决方案

#1


2  

Is this what you want?

这就是你想要的吗?

irb(main):001:0> a=[[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]

irb(main):002:0> h={}
=> {}

irb(main):003:0> a.each { |k,v| h[k] ||= []; h[k] << v }
=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]

irb(main):004:0> h
=> {1=>["A", "B"], 2=>["C", "D"]}

Or, if you really want a hashtable of hashtable with a 'result' key:

或者,如果您真的想要一个哈希表的哈希表,其中包含一个“result”键:

irb(main):003:0> a.each { |k,v| h[k] ||= {}; h[k]['result'] ||= []; h[k]['result'] << v }
=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]
irb(main):004:0> h
=> {1=>{"result"=>["A", "B"]}, 2=>{"result"=>["C", "D"]}}

#2


2  

For one-line lover:

一行的情人:

a = [[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
a.inject(Hash.new { |h, k| h[k] = {"results" => []} }) { |h, e| h[e.first]["results"] << e.last; h }

#3


1  

I'm not sure why you want that inner 'results' word, but here's how to get what you want:

我不知道你为什么想要那个内在的“结果”这个词,但以下是如何得到你想要的:

the_list = [[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
#=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]

by_key = the_list.group_by(&:first)
#=> {1=>[[1, "A"], [1, "B"]], 2=>[[2, "C"], [2, "D"]]}

as_result_hash = by_key.map do |key, matches|
  [key, {'results'=>matches.map(&:last) }]
end
#=> [[1, {"results"=>["A", "B"]}], [2, {"results"=>["C", "D"]}]]

final = Hash[*as_result_hash.flatten(1)]
#=> {1=>{"results"=>["A", "B"]}, 2=>{"results"=>["C", "D"]}}

It sounds like you already had figured out the basic usage of group_by - you can get a set of results grouped by some key.

听起来好像你已经找到了group_by的基本用法——你可以得到一组按某个键分组的结果。

The next step is to just map those results to the format you want. To do this, we just map the by_key dictionary, returning the original key, and the mapped results.

下一步是将这些结果映射到您想要的格式。为此,我们只需映射by_key字典,返回原始键和映射结果。

This returns an array, so we use Hash[*array.flatten(1)] to convert it back to a dictionary.

这返回一个数组,因此我们使用Hash[*array.flatten(1)]将它转换回字典。


if you don't need the inner 'results', you can just do:

如果你不需要内在的“结果”,你可以做:

as_result_hash = by_key.map do |key, matches|
  [key, matches.map(&:last)]
end
#=>  [[1, ["A", "B"]], [2, ["C", "D"]]]

#4


1  

a = [[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
Hash[a.group_by(&:first).map{ |k, v| [k, {"results" => v.map(&:last)}]}]

#1


2  

Is this what you want?

这就是你想要的吗?

irb(main):001:0> a=[[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]

irb(main):002:0> h={}
=> {}

irb(main):003:0> a.each { |k,v| h[k] ||= []; h[k] << v }
=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]

irb(main):004:0> h
=> {1=>["A", "B"], 2=>["C", "D"]}

Or, if you really want a hashtable of hashtable with a 'result' key:

或者,如果您真的想要一个哈希表的哈希表,其中包含一个“result”键:

irb(main):003:0> a.each { |k,v| h[k] ||= {}; h[k]['result'] ||= []; h[k]['result'] << v }
=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]
irb(main):004:0> h
=> {1=>{"result"=>["A", "B"]}, 2=>{"result"=>["C", "D"]}}

#2


2  

For one-line lover:

一行的情人:

a = [[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
a.inject(Hash.new { |h, k| h[k] = {"results" => []} }) { |h, e| h[e.first]["results"] << e.last; h }

#3


1  

I'm not sure why you want that inner 'results' word, but here's how to get what you want:

我不知道你为什么想要那个内在的“结果”这个词,但以下是如何得到你想要的:

the_list = [[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
#=> [[1, "A"], [1, "B"], [2, "C"], [2, "D"]]

by_key = the_list.group_by(&:first)
#=> {1=>[[1, "A"], [1, "B"]], 2=>[[2, "C"], [2, "D"]]}

as_result_hash = by_key.map do |key, matches|
  [key, {'results'=>matches.map(&:last) }]
end
#=> [[1, {"results"=>["A", "B"]}], [2, {"results"=>["C", "D"]}]]

final = Hash[*as_result_hash.flatten(1)]
#=> {1=>{"results"=>["A", "B"]}, 2=>{"results"=>["C", "D"]}}

It sounds like you already had figured out the basic usage of group_by - you can get a set of results grouped by some key.

听起来好像你已经找到了group_by的基本用法——你可以得到一组按某个键分组的结果。

The next step is to just map those results to the format you want. To do this, we just map the by_key dictionary, returning the original key, and the mapped results.

下一步是将这些结果映射到您想要的格式。为此,我们只需映射by_key字典,返回原始键和映射结果。

This returns an array, so we use Hash[*array.flatten(1)] to convert it back to a dictionary.

这返回一个数组,因此我们使用Hash[*array.flatten(1)]将它转换回字典。


if you don't need the inner 'results', you can just do:

如果你不需要内在的“结果”,你可以做:

as_result_hash = by_key.map do |key, matches|
  [key, matches.map(&:last)]
end
#=>  [[1, ["A", "B"]], [2, ["C", "D"]]]

#4


1  

a = [[1,"A"], [1,"B"], [2,"C"], [2,"D"]]
Hash[a.group_by(&:first).map{ |k, v| [k, {"results" => v.map(&:last)}]}]