如何将两个数组“压缩”为哈希

时间:2022-12-25 12:53:23

I want to "zip" two arrays into hash

我想将两个数组“压缩”成哈希

From:

从:

['BO','BR']
['BOLIVIA','BRAZIL']

To:

至:

{BO: 'BOLIVIA', BR:'BRAZIL'}

How to do it?

怎么做?

5 个解决方案

#1


16  

I would do it this way:

我会这样做:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']

Hash[keys.zip(values)]
# => {"BO"=>"BOLIVIA", "BR"=>"BRAZIL"}

If you want symbols for keys, then:

如果你想要键的符号,那么:

Hash[keys.map(&:to_sym).zip(values)]
# => {:BO=>"BOLIVIA", :BR=>"BRAZIL"}

In Ruby 2.1.0 or higher, you could write these as:

在Ruby 2.1.0或更高版本中,您可以将它们写为:

keys.zip(values).to_h
keys.map(&:to_sym).zip(values).to_h

#2


4  

Just use the single Array of the twos, and then transpose it, and generate Hash:

只需使用两个数组中的单个数组,然后转置它,并生成哈希:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']
Hash[[keys,values].transpose]
# => {"BO"=>"BOLIVIA", "BR"=>"BRAZIL"}

or for newer ruby version:

或者对于较新的红宝石版本:

[keys,values].transpose.to_h

#3


2  

Quite readable version would be:

相当可读的版本是:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']

keys.zip(values).each_with_object({}) do |(key, value), hash|
  hash[key.to_sym] = value
end

#4


2  

Ironically, if you just sprinkle some dots and underscores into your question, it just works:

具有讽刺意味的是,如果你只是在你的问题中撒上一些点和下划线,它就会起作用:

I want to "zip" two arrays into_hash

我想“拉”两个数组into_hash

ary1.zip(ary2).to_h
# => { 'BO' => 'BOLIVIA', 'BR' => 'BRAZIL' }

Actually, you specified in your output hash that the keys should be Symbols not Strings, so we need to convert them first:

实际上,你在输出哈希中指定键应该是符号而不是字符串,所以我们需要先转换它们:

ary1.map(&:to_sym).zip(ary2).to_h
# => { BO: 'BOLIVIA', BR: 'BRAZIL' }

#5


0  

You can make a zipped array and then convert the array into hash like so :

您可以创建一个压缩数组,然后将数组转换为哈希,如下所示:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']    
array = key.zip(values) # => [['BO','BOLIVIA'],['BR','BRAZIL']]
hash = array.to_h # => {'BO' => 'BOLIVIA','BR' => 'BRAZIL'}

#1


16  

I would do it this way:

我会这样做:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']

Hash[keys.zip(values)]
# => {"BO"=>"BOLIVIA", "BR"=>"BRAZIL"}

If you want symbols for keys, then:

如果你想要键的符号,那么:

Hash[keys.map(&:to_sym).zip(values)]
# => {:BO=>"BOLIVIA", :BR=>"BRAZIL"}

In Ruby 2.1.0 or higher, you could write these as:

在Ruby 2.1.0或更高版本中,您可以将它们写为:

keys.zip(values).to_h
keys.map(&:to_sym).zip(values).to_h

#2


4  

Just use the single Array of the twos, and then transpose it, and generate Hash:

只需使用两个数组中的单个数组,然后转置它,并生成哈希:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']
Hash[[keys,values].transpose]
# => {"BO"=>"BOLIVIA", "BR"=>"BRAZIL"}

or for newer ruby version:

或者对于较新的红宝石版本:

[keys,values].transpose.to_h

#3


2  

Quite readable version would be:

相当可读的版本是:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']

keys.zip(values).each_with_object({}) do |(key, value), hash|
  hash[key.to_sym] = value
end

#4


2  

Ironically, if you just sprinkle some dots and underscores into your question, it just works:

具有讽刺意味的是,如果你只是在你的问题中撒上一些点和下划线,它就会起作用:

I want to "zip" two arrays into_hash

我想“拉”两个数组into_hash

ary1.zip(ary2).to_h
# => { 'BO' => 'BOLIVIA', 'BR' => 'BRAZIL' }

Actually, you specified in your output hash that the keys should be Symbols not Strings, so we need to convert them first:

实际上,你在输出哈希中指定键应该是符号而不是字符串,所以我们需要先转换它们:

ary1.map(&:to_sym).zip(ary2).to_h
# => { BO: 'BOLIVIA', BR: 'BRAZIL' }

#5


0  

You can make a zipped array and then convert the array into hash like so :

您可以创建一个压缩数组,然后将数组转换为哈希,如下所示:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']    
array = key.zip(values) # => [['BO','BOLIVIA'],['BR','BRAZIL']]
hash = array.to_h # => {'BO' => 'BOLIVIA','BR' => 'BRAZIL'}