将键数组和值数组转换为Ruby中的散列

时间:2021-01-11 21:17:17

I have two arrays like this:

我有两个这样的数组:

keys = ['a', 'b', 'c']
values = [1, 2, 3]

Is there a simple way in Ruby to convert those arrays into the following hash?

Ruby中是否有一种简单的方法可以将这些数组转换成下面的散列?

{ 'a' => 1, 'b' => 2, 'c' => 3 }

Here is my way of doing it, but I feel like there should be a built-in method to easily do this.

这是我的方法,但我觉得应该有一个内置的方法来轻松地做到这一点。

def arrays2hash(keys, values)
  hash = {}
  0.upto(keys.length - 1) do |i|
      hash[keys[i]] = values[i]
  end
  hash
end

5 个解决方案

#1


50  

The following works in 1.8.7:

1.8.7的工作内容如下:

keys = ["a", "b", "c"]
values = [1, 2, 3]
zipped = keys.zip(values)
=> [["a", 1], ["b", 2], ["c", 3]]
Hash[zipped]
=> {"a"=>1, "b"=>2, "c"=>3}

This appears not to work in older versions of Ruby (1.8.6). The following should be backwards compatible:

这似乎不适用于老版本的Ruby(1.8.6)。以下应向后兼容:

Hash[*keys.zip(values).flatten]

#2


8  

Another way is to use each_with_index:

另一种方法是使用each_with_index:

hash = {}
keys.each_with_index { |key, index| hash[key] = values[index] }

hash # => {"a"=>1, "b"=>2, "c"=>3}

#3


5  

The same can be done using Array#transpose method. If you are using Ruby version >= 2.1, you can take the advantage of the method Array#to_h, otherwise use your old friend, Hash::[]

同样可以使用数组#转置方法。如果您使用的是Ruby版本>= 2.1,您可以利用方法数组#to_h的优点,否则使用您的老朋友哈希::[]

keys = ['a', 'b', 'c']
values = [1, 2, 3]
[keys, values].transpose.to_h
# => {"a"=>1, "b"=>2, "c"=>3}
Hash[[keys, values].transpose]
# => {"a"=>1, "b"=>2, "c"=>3}

#4


1  

See this SO Q&A: What is a Ruby equivalent for Python’s “zip” builtin?

看看这个问题:什么是Ruby对Python的“zip”构建的等效?

#5


0  

Try this, this way the latter one d will overwrite the former one c

试试这个,这样后一个d会覆盖前一个c

irb(main):001:0>  hash = Hash[[[1,2,3,3], ['a','b','c','d']].transpose]
=> {1=>"a", 2=>"b", 3=>"d"}
irb(main):002:0>

#1


50  

The following works in 1.8.7:

1.8.7的工作内容如下:

keys = ["a", "b", "c"]
values = [1, 2, 3]
zipped = keys.zip(values)
=> [["a", 1], ["b", 2], ["c", 3]]
Hash[zipped]
=> {"a"=>1, "b"=>2, "c"=>3}

This appears not to work in older versions of Ruby (1.8.6). The following should be backwards compatible:

这似乎不适用于老版本的Ruby(1.8.6)。以下应向后兼容:

Hash[*keys.zip(values).flatten]

#2


8  

Another way is to use each_with_index:

另一种方法是使用each_with_index:

hash = {}
keys.each_with_index { |key, index| hash[key] = values[index] }

hash # => {"a"=>1, "b"=>2, "c"=>3}

#3


5  

The same can be done using Array#transpose method. If you are using Ruby version >= 2.1, you can take the advantage of the method Array#to_h, otherwise use your old friend, Hash::[]

同样可以使用数组#转置方法。如果您使用的是Ruby版本>= 2.1,您可以利用方法数组#to_h的优点,否则使用您的老朋友哈希::[]

keys = ['a', 'b', 'c']
values = [1, 2, 3]
[keys, values].transpose.to_h
# => {"a"=>1, "b"=>2, "c"=>3}
Hash[[keys, values].transpose]
# => {"a"=>1, "b"=>2, "c"=>3}

#4


1  

See this SO Q&A: What is a Ruby equivalent for Python’s “zip” builtin?

看看这个问题:什么是Ruby对Python的“zip”构建的等效?

#5


0  

Try this, this way the latter one d will overwrite the former one c

试试这个,这样后一个d会覆盖前一个c

irb(main):001:0>  hash = Hash[[[1,2,3,3], ['a','b','c','d']].transpose]
=> {1=>"a", 2=>"b", 3=>"d"}
irb(main):002:0>