Ruby: Invert a hash to also preserve non unique va

2019-07-21 00:38发布

问题:

I have a hash that looks like this:

{"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5], "d" => [7, 2, 3]}

What I want to do is to make a hash of all existing values with an array of all keys that included it, e.g. turn the above into this:

{1 => ["a"], 2 => ["a", "d"], 3 => ["a", "c", "d"], 4 => ["b", "c"]}

回答1:

I do prefer @Jikku's solution, but there's always another way. Here's one. [Edit: I see this is very close to @Chris's solution. I will leave it for the last line, which is a little different.]

Code

def inside_out(h)
  g = h.flat_map { |s,a| a.product([s]) }
       .group_by(&:first)
  g.merge(g) { |_,a| a.map(&:last) }
end

Example

h = {"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5], "d" => [7, 2, 3]}

inside_out(h)
  #=> {1=>["a"], 2=>["a", "d"], 3=>["a", "c", "d"], 4=>["b", "c"],
  #    5=>["b", "c"], 6=>["b"], 7=>["d"]} 

Explanation

For h above:

a = h.flat_map { |s,a| a.product([s]) }
  #=> [[1, "a"], [2, "a"], [3, "a"], [4, "b"], [5, "b"], [6, "b"],
  #    [3, "c"], [4, "c"], [5, "c"], [7, "d"], [2, "d"], [3, "d"]] 
g = a.group_by(&:first)
  #=> {1=>[[1, "a"]], 2=>[[2, "a"], [2, "d"]],
  #    3=>[[3, "a"], [3, "c"], [3, "d"]],
  #    4=>[[4, "b"], [4, "c"]],
  #    5=>[[5, "b"], [5, "c"]],
  #    6=>[[6, "b"]],
  #    7=>[[7, "d"]]} 
g.merge(g) { |_,a| a.map(&:last) }
  #=> {1=>["a"], 2=>["a", "d"], 3=>["a", "c", "d"], 4=>["b", "c"],
  #    5=>["b", "c"], 6=>["b"], 7=>["d"]} 


回答2:

Try this:

module HashReverser
  def invert_map
    each_with_object({}) do |(key, value), result|
      value.each { |v| (result[v] ||= []) << key }
    end
  end
end

original = {"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5]}

original.extend(HashReverser).invert_map # => {1=>["a"], 2=>["a"], 3=>["a", "c"], 4=>["b", "c"], 5=>["b", "c"], 6=>["b"]}


回答3:

An alternate solution:

# Given
h = {"a" => [1, 2, 3], "b" => [4, 5, 6], "c" => [3, 4, 5], "d" => [7, 2, 3]}

h.flat_map {|k, v| v.product [k]}.group_by(&:first).each_value {|v| v.map! &:last }

Or:

h.flat_map {|k, v| v.product [k]}.reduce({}) {|o, (k, v)| (o[k] ||= []) << v; o}

The idea here is that we use Array#product to create a list of inverted single key-value pairs:

product = h.flat_map {|k, v| v.product([k]) }
# => [[1, "a"], [2, "a"], [3, "a"], [4, "b"], [5, "b"], [6, "b"], [3, "c"], [4, "c"], [5, "c"], [7, "d"], [2, "d"], [3, "d"]]

Group them by the value of the first item in each pair:

groups = product.group_by(&:first)
# => {1=>[[1, "a"]], 2=>[[2, "a"], [2, "d"]], 3=>[[3, "a"], [3, "c"], [3, "d"]], 4=>[[4, "b"], [4, "c"]], 5=>[[5, "b"], [5, "c"]], 6=>[[6, "b"]], 7=>[[7, "d"]]}

And then convert the values to a list of the last values in each pair:

result = groups.each_value {|v| v.map! &:last }
# => {1=>["a"], 2=>["a", "d"], 3=>["a", "c", "d"], 4=>["b", "c"], 5=>["b", "c"], 6=>["b"], 7=>["d"]}


标签: ruby hash