我尝试搜索,但任何解决方案都不起作用。
我有这个哈希数组
[{:smartphone=>"Galaxy Note 8 - Samsung", :votes=>0}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>0}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>1}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>0}, {:smartphone=>"iPhone X - Apple", :votes=>0}, {:smartphone=>"iPhone X - Apple", :votes=>1}, {:smartphone=>"iPhone X - Apple", :votes=>1}, {:smartphone=>"Galaxy Note 8 - Samsung", :votes=>0}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>0}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>0}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>0}, {:smartphone=>"Galaxy Note 8 - Samsung", :votes=>0}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>0}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>0}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>0}, {:smartphone=>"Galaxy Note 8 - Samsung", :votes=>0}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>0}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>0}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>1}, {:smartphone=>"iPhone X - Apple", :votes=>0}, {:smartphone=>"Galaxy Note 8 - Samsung", :votes=>0}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>1}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>0}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>0}, {:smartphone=>"iPhone X - Apple", :votes=>0}, {:smartphone=>"Galaxy Note 8 - Samsung", :votes=>1}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>0}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>0}, {:smartphone=>"Galaxy Note 8 - Samsung", :votes=>0}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>1}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"iPhone X - Apple", :votes=>0}, {:smartphone=>"iPhone X - Apple", :votes=>0}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}]
我想为每部智能手机总结选票</ p>
我尝试
results.group_by { |hash1| hash1[:smartphone] }.map do |_,v|
v.inject do |ele_hash2,ele_hash1|
puts ele_hash2
puts ele_hash1
ele_hash2.merge(ele_hash1) {|k,o,n| k == :vote ? o+n : o }
end
end
但它让我回头
[{:smartphone=>"Galaxy Note 8 - Samsung", :votes=>0}, {:smartphone=>"ZenFone 4 Pro - Asus", :votes=>0}, {:smartphone=>"Xperia XZ1 - Sony", :votes=>0}, {:smartphone=>"Mate 10 Pro - Huawei", :votes=>0}, {:smartphone=>"V30 - LG", :votes=>0}, {:smartphone=>"One Plus 5T", :votes=>0}, {:smartphone=>"Pixel 2 XL - Google", :votes=>1}, {:smartphone=>"Nokia 8", :votes=>0}, {:smartphone=>"Z2 Force - Moto", :votes=>0}, {:smartphone=>"U11 - HTC", :votes=>0}, {:smartphone=>"iPhone X - Apple", :votes=>0}]
并且这不正确,例如Apple iPhone X有1票。 为什么呢?
答案 0 :(得分:1)
如果arr
是您的哈希数组,则可以执行以下操作。
arr.each_with_object(Hash.new(0)) { |g,h| h[g[:smartphone]] += g[:votes] }
#=> {"Galaxy Note 8 - Samsung"=>1, "ZenFone 4 Pro - Asus"=>1,
# "Xperia XZ1 - Sony"=>0, "Mate 10 Pro - Huawei"=>0, "V30 - LG"=>1,
# "One Plus 5T"=>0, "Pixel 2 XL - Google"=>1, "Nokia 8"=>0,
# "Z2 Force - Moto"=>0, "U11 - HTC"=>1, "iPhone X - Apple"=>2}
Hash.new(0)
创建一个默认值为零的哈希值(有时称为计算哈希值)。见Hash::new。这意味着如果如此定义的散列h
没有键k
,则h[k]
将返回默认值,此处为零。 (散列不会更改。)表达式h[g[:smartphone]] += g[:votes]
扩展为
h[g[:smartphone]] = h[g[:smartphone]] + g[:votes]
如果数组中的散列g
的键"iPhone X - Apple"
的值为:smartphone
,则上面的表达式变为
h["iPhone X - Apple"] = h["iPhone X - Apple"] + g[:votes]
第一次遇到"iPhone X - Apple"
:smartphone
的值g
时,正在构建的哈希h
没有密钥"iPhone X - Apple"
。因此,在上面的等式右侧的h["iPhone X - Apple"]
设置为等于默认值零,所以我们有
h["iPhone X - Apple"] = g[:votes]
下次g[:smartphone]
被发现等于"iPhone X - Apple"
时,相等权限右侧的h["iPhone X - Apple"]
将等于g[:votes]
,因此默认值将不适用。< / p>
答案 1 :(得分:0)
input.
group_by { |h| h[:smartphone] }.
map { |k, v| [k, v.reduce(0) { |acc, h| acc + h[:votes] }] }.
to_h
#⇒ {
# "Galaxy Note 8 - Samsung" => 1,
# "Mate 10 Pro - Huawei" => 0,
# "Nokia 8" => 0,
# "One Plus 5T" => 0,
# "Pixel 2 XL - Google" => 1,
# "U11 - HTC" => 1,
# "V30 - LG" => 1,
# "Xperia XZ1 - Sony" => 0,
# "Z2 Force - Moto" => 0,
# "ZenFone 4 Pro - Asus" => 1,
# "iPhone X - Apple" => 2
# }
或者,为了获得您想要的确切结果:
input.
group_by { |h| h[:smartphone] }.
map do |k, v|
{smartphone: k, votes: v.reduce(0) { |acc, h| acc + h[:votes] }}
end
您的代码不起作用,因为在Hash#merge
内你有一个拼写错误k == :vote
应该阅读k == :votes
。