gpt4 book ai didi

ruby - 如何计算 Ruby 1.8.5 中的重复哈希项(Sketchup Ruby API)

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:15 24 4
gpt4 key购买 nike

我需要计算重复项,它们需要 100% 相同才能增加我的计数,但我不能在 Ruby 1.8.5 中使用 nothing,此代码将在 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}
];

最佳答案

简答:

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 中的键的数组,这只是 hash 中的唯一产品你的 list 。然后函数映射用结果替换该数组中的每个对象后面的 block 只是将计数值添加到产品哈希中。

关于ruby - 如何计算 Ruby 1.8.5 中的重复哈希项(Sketchup Ruby API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12236656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com