1

我需要计算重复项,它们需要 100% 相同才能增加我的计数,但我不能使用 Ruby 1.8.5 中的任何内容,此代码将在 google sketchup 的插件中运行

谷歌 Sketchup Ruby 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}
                      ];
4

2 回答 2

3

简短的回答:

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 从哈希 h 返回一个键数组,这只是您列表中的唯一产品。然后,函数映射将该数组中的每个对象替换为后面的块的结果,该块的结果只是将计数值添加到产品哈希中。

于 2012-09-02T15:14:19.533 回答
0
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

于 2012-09-02T15:41:59.123 回答