Ruby 从数组中提取值
从下面的散列中,需要提取每个参数的值:Ruby 从数组中提取值,ruby,arrays,Ruby,Arrays,从下面的散列中,需要提取每个参数的值: array_of_hashes = [{type: "test1", value: 1}, {type: "test1", value: 1}, {type: "test2", value: 1}, {type: "test2", value: 1}] 我想实现这样的目标: array_of_hashes = [{"test1" => [{type: "test1", value: 1}, {type: "test1", value: 1}}],
array_of_hashes = [{type: "test1", value: 1}, {type: "test1", value: 1}, {type: "test2", value: 1}, {type: "test2", value: 1}]
我想实现这样的目标:
array_of_hashes = [{"test1" => [{type: "test1", value: 1}, {type: "test1", value: 1}}], {"test2" => {type: "test2", value: 1}, {type: "test2", value: 1}}]
转换此阵列最有效的方法是什么?此数组可能有大约2 000 000个值。您可以使用:
如本例所示,如果哈希值按
:type
的值排序,您可能会发现(然后转换为哈希值)比group\u by
快:
Hash[array_of_hashes.chunk { |h| h[:type] }.to_a]
我建议你比较一下编辑:我已测试。如下图所示,group_by
似乎更快
def mk_arr(n,m)
n.times.with_object([]) { |i,a| m.times { a << { type: "test#{i}", value: 1 } } }
end
mk_arr(3,2)
#=> [{:type=>"test0", :value=>1}, {:type=>"test0", :value=>1},
# {:type=>"test1", :value=>1}, {:type=>"test1", :value=>1},
# {:type=>"test2", :value=>1}, {:type=>"test2", :value=>1}]
require 'benchmark'
def bench_em(n,m)
arr = mk_arr(n,m)
puts "n = #{n}, m = #{m}"
print "group_by: "
puts Benchmark.measure { arr.group_by { |h| h[:type] } }
print "chunk : "
puts Benchmark.measure { Hash[arr.chunk { |h| h[:type] }.to_a] }
puts
end
user system total real
n = 100000, m = 2
group_by: 0.090000 0.000000 0.090000 ( 0.095301)
chunk : 0.160000 0.000000 0.160000 ( 0.166608)
n = 100000, m = 4
group_by: 0.250000 0.010000 0.260000 ( 0.252951)
chunk : 0.260000 0.000000 0.260000 ( 0.261747)
n = 100000, m = 8
group_by: 0.370000 0.010000 0.380000 ( 0.386423)
chunk : 0.450000 0.000000 0.450000 ( 0.447134)
n = 1000000, m = 4
group_by: 2.740000 0.040000 2.780000 ( 2.775030)
chunk : 3.690000 0.070000 3.760000 ( 3.790305)
def mk_arr(n,m)
n、 times.with_object([]){i,a | m.times{a[{:type=>“test0”,:value=>1},{:type=>“test0”,:value=>1},
#{:type=>test1',:value=>1},{:type=>test1',:value=>1},
#{:type=>test2',:value=>1},{:type=>test2',:value=>1}]
需要“基准”
def台架(n,m)
arr=mk_arr(n,m)
放置“n={n},m={m}”
打印“分组依据:”
将Benchmark.measure{arr.group_按{h | h[:type]}放置
打印“块”:
将Benchmark.measure{Hash[arr.chunk{| h | h[:type]}.to_a]}
放
结束
用户系统总真实值
n=100000,m=2
分组依据:0.090000 0.0000000.090000(0.095301)
区块:0.160000 0.0000000.160000(0.166608)
n=100000,m=4
分组依据:0.250000.010000 0.260000(0.252951)
区块:0.260000.0000000.260000(0.261747)
n=100000,m=8
分组依据:0.370000 0.010000 0.380000(0.386423)
区块:0.450000 0.0000000.450000(0.447134)
n=1000000,m=4
组别:2.740000 0.040000 2.780000(2.775030)
区块:3.690000 0.070000 3.760000(3.790305)
def mk_arr(n,m)
n.times.with_object([]) { |i,a| m.times { a << { type: "test#{i}", value: 1 } } }
end
mk_arr(3,2)
#=> [{:type=>"test0", :value=>1}, {:type=>"test0", :value=>1},
# {:type=>"test1", :value=>1}, {:type=>"test1", :value=>1},
# {:type=>"test2", :value=>1}, {:type=>"test2", :value=>1}]
require 'benchmark'
def bench_em(n,m)
arr = mk_arr(n,m)
puts "n = #{n}, m = #{m}"
print "group_by: "
puts Benchmark.measure { arr.group_by { |h| h[:type] } }
print "chunk : "
puts Benchmark.measure { Hash[arr.chunk { |h| h[:type] }.to_a] }
puts
end
user system total real
n = 100000, m = 2
group_by: 0.090000 0.000000 0.090000 ( 0.095301)
chunk : 0.160000 0.000000 0.160000 ( 0.166608)
n = 100000, m = 4
group_by: 0.250000 0.010000 0.260000 ( 0.252951)
chunk : 0.260000 0.000000 0.260000 ( 0.261747)
n = 100000, m = 8
group_by: 0.370000 0.010000 0.380000 ( 0.386423)
chunk : 0.450000 0.000000 0.450000 ( 0.447134)
n = 1000000, m = 4
group_by: 2.740000 0.040000 2.780000 ( 2.775030)
chunk : 3.690000 0.070000 3.760000 ( 3.790305)