如何计算重复在Ruby 1.8.5中使用hashetting(Sketchup Ruby API) [英] How to count duplicates hash itens in Ruby 1.8.5 ( Sketchup Ruby API )
问题描述
我需要计算重复项,它们需要100%完全相同才能增加我的计数,但我无法使用Ruby 1.8.5中的任何内容,此代码将在Google Sketchup中的插件中运行
I need to count the duplicates, they need to be 100% identical to increase my count, but I can not use a nothing out of Ruby 1.8.5, this code will run inside a plugin in google sketchup
puts VERSION
1.8.5
puts RUBY_PLATFORM
i686-darwin8.10.1
product = 'Glass'
x = width
y = length
z = density
product_list = [
{ "product" => 1, "x" => 200, "y" => 100, "z" => 18},
{ "product" => 1, "x" => 200, "y" => 100, "z" => 18},
{ "product" => 1, "x" => 300, "y" => 100, "z" => 18},
{ "product" => 2, "x" => 300, "y" => 100, "z" => 18},
{ "product" => 2, "x" => 100, "y" => 100, "z" => 18},
{ "product" => 2, "x" => 100, "y" => 100, "z" => 18},
{ "product" => 3, "x" => 100, "y" => 100, "z" => 18}
];
product_list_result = product_list.count_duplicate();
product_list_result = [
{ "product" => 1, "x" => 200, "y" => 100, "z" => 18, "count" = 2},
{ "product" => 1, "x" => 300, "y" => 100, "z" => 18, "count" = 1},
{ "product" => 2, "x" => 300, "y" => 100, "z" => 18, "count" = 1},
{ "product" => 2, "x" => 100, "y" => 100, "z" => 18, "count" = 2},
{ "product" => 3, "x" => 100, "y" => 100, "z" => 18, "count" = 1}
];
推荐答案
简短回答:
Short answer:
h = Hash.new 0
product_list.each {|p| h[p] += 1}
product_list_result = h.keys.map{|k| k["count"] = h[k]; k}
更长的答案解释了这是如何工作的。
从您的数据开始:
Longer answer explaining how this works. Starting with your data:
product_list = [
{ "product" => 1, "x" => 200, "y" => 100, "z" => 18},
{ "product" => 1, "x" => 200, "y" => 100, "z" => 18},
{ "product" => 1, "x" => 300, "y" => 100, "z" => 18},
{ "product" => 2, "x" => 300, "y" => 100, "z" => 18},
{ "product" => 2, "x" => 100, "y" => 100, "z" => 18},
{ "product" => 2, "x" => 100, "y" => 100, "z" => 18},
{ "product" => 3, "x" => 100, "y" => 100, "z" => 18}
];
# First, create a hash to count the number of unique products. Have the initial
# count be 0.
h = Hash.new 0
# Add each product to the hash count.
product_list.each {|p| h[p] += 1}
现在你有一个产品作为键的散列,值:
Now you have a hash with products as keys, and counts as values:
h = {{"z"=>18, "y"=>100, "x"=>100, "product"=>3}=>1, {"z"=>18, "y"=>100, "x"=>300, "product"=>1}=>1, {"z"=>18, "y"=>100, "x"=>200, "product"=>1}=>2, {"z"=>18, "y"=>100, "x"=>300, "product"=>2}=>1, {"z"=>18, "y"=>100, "x"=>100, "product"=>2}=>2}
它以你想要的数组格式:
Now convert it to the array format you desire:
product_list_result = []
h.keys.each do |k|
# since each key is a product hash, we can add count to it
k["count"] = h[k]
# Now, add that to the array
product_list_result << k
end
结果如下:
Which results in:
product_list_result = [
{"z"=>18, "y"=>100, "x"=>100, "product"=>3, "count"=>1},
{"z"=>18, "y"=>100, "x"=>300, "product"=>1, "count"=>1},
{"z"=>18, "y"=>100, "x"=>200, "product"=>1, "count"=>2},
{"z"=>18, "y"=>100, "x"=>300, "product"=>2, "count"=>1},
{"z"=>18, "y"=>100, "x"=>100, "product"=>2, "count"=>2}
]
数组转换可以更简洁地完成:
The array conversion can be done more succinctly:
product_list_result = h.keys.map{|k| k["count"] = h[k]; k}
您的清单。函数映射然后用该块后面的结果
替换该数组中的每个对象,该结果只是将计数值添加到产品散列。
h.keys returns an array of keys from hash h, which is just the unique products in your list. The function map then replaces each object in that array with the result of the block that follows which simply adds the count value to the product hash.
这篇关于如何计算重复在Ruby 1.8.5中使用hashetting(Sketchup Ruby API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!