如何计算重复哈希itens用Ruby 1.8.5(SketchUp的红宝石API)(How to c

2019-06-27 16:52发布

我需要统计重复,他们需要的是100%相同,以增加我的计算,但我不能用什么去的Ruby 1.8.5的,这个代码将在谷歌的SketchUp插件内运行

谷歌SketchUp的红宝石API

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}
                      ];

Answer 1:

简短的回答:

h = Hash.new 0
product_list.each {|p| h[p] += 1}
product_list_result = h.keys.map{|k| k["count"] = h[k]; k}

较长的回答解释了这一过程。 与您的数据开始:

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}

现在,你必须以产品为钥匙,算作值的哈希:

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}

现在它转换成你想要的格式排列:

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

这会导致:

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}
                      ]

阵列转换可以更简洁地完成:

product_list_result = h.keys.map{|k| k["count"] = h[k]; k}

h.keys返回键从哈希小时,这仅仅是在列表中的独特的产品阵列。 所述函数映射然后替换每个对象阵列与如下简单地增加的计数值到产品散列块的结果英寸



Answer 2:

product_list.dup.group_by { |h| h }.each_value.map do |value| 
  value.first.tap { |hash| hash['count'] = value.count }
end

=> [{"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}]

dup是不修改原来的product_list

经测试,在ruby 1.8.7



文章来源: How to count duplicates hash itens in Ruby 1.8.5 ( Sketchup Ruby API )