如何将数组转换为哈希和另一个数组的键到相同哈希的值?

时间:2021-09-01 21:16:14

I have 2 arrays:

我有2个数组:

array1 = ["H", "e", "l", "l", "o"]
array2 = ["o", "l", "l", "e", "H"]

I want the array1 elements to become the keys in a new Hash, and the array2 elements to become the corresponding values in the same Hash. Can someone please suggest how to do this?

我希望array1元素成为新Hash中的键,并且array2元素成为相同Hash中的对应值。有人可以建议怎么做吗?

Thanks

4 个解决方案

#1


4  

array1 = ["H", "e", "l", "l", "o"]
array2 = ["o", "l", "l", "e", "H"]
p Hash[array1.zip(array2)]
# >> {"H"=>"o", "e"=>"l", "l"=>"e", "o"=>"H"}

#2


0  

There are two "l"s in the "key" array, while Hashes can't have duplicate keys. Any solution will have either "l" => "e" or "l" => "l", but not both. If that's okay, then @RubyLovely's solution is great. If you want to preserve a mapping for each pair, an Array of Hashes might be appropriate:

“key”数组中有两个“l”,而Hashes不能有重复的键。任何解决方案都将具有“l”=>“e”或“l”=>“l”,但不能同时具有两者。如果没关系,那么@ RubyLovely的解决方案很棒。如果要为每对保留映射,则可能需要使用哈希数组:

array1 = ["H", "e", "l", "l", "o"]
array2 = ["o", "l", "l", "e", "H"]
array1.zip(array2).map{|pair| Hash[*pair]}
# => [{"H"=>"o"}, {"e"=>"l"}, {"l"=>"l"}, {"l"=>"e"}, {"o"=>"H"}]

#3


0  

The solution by @RubyLovely is how I would do it but for the sake of variety here is another solution:

@RubyLovely的解决方案是我如何做到的,但为了多样性,这里是另一个解决方案:

array1.each_with_index.reduce({}) do |memo,(x,i)|
  memo[x] = array2[i]; memo
end
# => {"H"=>"o", "e"=>"l", "l"=>"e", "o"=>"H"} 

#4


0  

Doing it this way, you would not need to create an intermediate array, and hence would be more effective than doing so.

这样做,您不需要创建一个中间数组,因此比这样做更有效。

h = {}
array1.zip(array2){|k, v| h[k] = v}
h # => {"H"=>"o", "e"=>"l", "l"=>"e", "o"=>"H"}

#1


4  

array1 = ["H", "e", "l", "l", "o"]
array2 = ["o", "l", "l", "e", "H"]
p Hash[array1.zip(array2)]
# >> {"H"=>"o", "e"=>"l", "l"=>"e", "o"=>"H"}

#2


0  

There are two "l"s in the "key" array, while Hashes can't have duplicate keys. Any solution will have either "l" => "e" or "l" => "l", but not both. If that's okay, then @RubyLovely's solution is great. If you want to preserve a mapping for each pair, an Array of Hashes might be appropriate:

“key”数组中有两个“l”,而Hashes不能有重复的键。任何解决方案都将具有“l”=>“e”或“l”=>“l”,但不能同时具有两者。如果没关系,那么@ RubyLovely的解决方案很棒。如果要为每对保留映射,则可能需要使用哈希数组:

array1 = ["H", "e", "l", "l", "o"]
array2 = ["o", "l", "l", "e", "H"]
array1.zip(array2).map{|pair| Hash[*pair]}
# => [{"H"=>"o"}, {"e"=>"l"}, {"l"=>"l"}, {"l"=>"e"}, {"o"=>"H"}]

#3


0  

The solution by @RubyLovely is how I would do it but for the sake of variety here is another solution:

@RubyLovely的解决方案是我如何做到的,但为了多样性,这里是另一个解决方案:

array1.each_with_index.reduce({}) do |memo,(x,i)|
  memo[x] = array2[i]; memo
end
# => {"H"=>"o", "e"=>"l", "l"=>"e", "o"=>"H"} 

#4


0  

Doing it this way, you would not need to create an intermediate array, and hence would be more effective than doing so.

这样做,您不需要创建一个中间数组,因此比这样做更有效。

h = {}
array1.zip(array2){|k, v| h[k] = v}
h # => {"H"=>"o", "e"=>"l", "l"=>"e", "o"=>"H"}