How to initialize a hash with keys from an array like the following?
如何使用数组中的键初始化哈希,如下所示?
keys = [ 'a' , 'b' , 'c' ]
Desired hash h
should be:
期望的哈希值应该是:
puts h
# { 'a' => nil , 'b' => nil , 'c' => nil }
5 个解决方案
#1
8
Here we go using Enumerable#each_with_object
and Hash::[]
.
这里我们使用Enumerable#each_with_object和Hash :: []。
keys = [ 'a' , 'b' , 'c' ]
Hash[keys.each_with_object(nil).to_a]
# => {"a"=>nil, "b"=>nil, "c"=>nil}
or Using Array#product
或使用Array#product
keys = [ 'a' , 'b' , 'c' ]
Hash[keys.product([nil])]
# => {"a"=>nil, "b"=>nil, "c"=>nil}
#2
6
Another alternative using Array#zip
:
另一种使用Array#zip的替代方案:
Hash[keys.zip([])]
# => {"a"=>nil, "b"=>nil, "c"=>nil}
Update: As suggested by Arup Rakshit here's a performance comparison between the proposed solutions:
更新:根据Arup Rakshit的建议,这里提出的解决方案之间的性能比较:
require 'fruity'
ary = *(1..10_000)
compare do
each_with_object { ary.each_with_object(nil).to_a }
product { ary.product([nil]) }
zip { ary.zip([]) }
map { ary.map { |k| [k, nil] } }
end
The result:
Running each test once. Test will take about 1 second.
zip is faster than product by 19.999999999999996% ± 1.0%
product is faster than each_with_object by 30.000000000000004% ± 1.0%
each_with_object is similar to map
#3
4
Using the new (Ruby 2.1) to_h
:
使用新的(Ruby 2.1)to_h:
keys.each_with_object(nil).to_h
#4
1
=> keys = [ 'a' , 'b' , 'c' ]
=> Hash[keys.map { |x, z| [x, z] }]
# {"a"=>nil, "b"=>nil, "c"=>nil}
=> Hash[keys.map { |x| [x, nil] }]
# {"a"=>nil, "b"=>nil, "c"=>nil}
=> Hash[keys.map { |x, _| [x, _] }]
# {"a"=>nil, "b"=>nil, "c"=>nil}
#5
0
I think you should try:
我想你应该试试:
x = {}
keys.map { |k, _| x[k.to_s] = _ }
#1
8
Here we go using Enumerable#each_with_object
and Hash::[]
.
这里我们使用Enumerable#each_with_object和Hash :: []。
keys = [ 'a' , 'b' , 'c' ]
Hash[keys.each_with_object(nil).to_a]
# => {"a"=>nil, "b"=>nil, "c"=>nil}
or Using Array#product
或使用Array#product
keys = [ 'a' , 'b' , 'c' ]
Hash[keys.product([nil])]
# => {"a"=>nil, "b"=>nil, "c"=>nil}
#2
6
Another alternative using Array#zip
:
另一种使用Array#zip的替代方案:
Hash[keys.zip([])]
# => {"a"=>nil, "b"=>nil, "c"=>nil}
Update: As suggested by Arup Rakshit here's a performance comparison between the proposed solutions:
更新:根据Arup Rakshit的建议,这里提出的解决方案之间的性能比较:
require 'fruity'
ary = *(1..10_000)
compare do
each_with_object { ary.each_with_object(nil).to_a }
product { ary.product([nil]) }
zip { ary.zip([]) }
map { ary.map { |k| [k, nil] } }
end
The result:
Running each test once. Test will take about 1 second.
zip is faster than product by 19.999999999999996% ± 1.0%
product is faster than each_with_object by 30.000000000000004% ± 1.0%
each_with_object is similar to map
#3
4
Using the new (Ruby 2.1) to_h
:
使用新的(Ruby 2.1)to_h:
keys.each_with_object(nil).to_h
#4
1
=> keys = [ 'a' , 'b' , 'c' ]
=> Hash[keys.map { |x, z| [x, z] }]
# {"a"=>nil, "b"=>nil, "c"=>nil}
=> Hash[keys.map { |x| [x, nil] }]
# {"a"=>nil, "b"=>nil, "c"=>nil}
=> Hash[keys.map { |x, _| [x, _] }]
# {"a"=>nil, "b"=>nil, "c"=>nil}
#5
0
I think you should try:
我想你应该试试:
x = {}
keys.map { |k, _| x[k.to_s] = _ }