如果我有一个数组
private void readItems() {
File filesDir = getFilesDir();
File locationFile = new File(filesDir, "locations.txt");
try {
items = new ArrayList<UserLocation>(FileUtils.readLines(locationFile));
} catch (IOException e) {
items = new ArrayList<locationFile>();
}
}
如何沿着以下行返回一些东西,它通过数组并根据数组中的一个属性计算总数?
apple_array = [
#<name: "Bob", apples_eaten: 3>,
#<name: "Robert", apples_eaten: 7>,
#<name: "Bob", apples_eaten: 5>,
#<name: "Rebecca", apples_eaten: 2>,
#<name: "Robert", apples_eaten: 3>
]
现在我知道如何通过调用name: Bob, apples_eaten: 8
name: Robert, apples_eaten: 10
name: Rebecca, apples_eaten: 2
来返回apples_eaten
的总金额,有没有办法将该行映射到每个姓名?
答案 0 :(得分:1)
将它们全部放入映射名称=&gt;的新哈希中individual_total:
totals = Hash.new(0)
apple_array.each do |a|
totals[a.name] += a.apples_eaten
end
答案 1 :(得分:1)
apple_array.inject(Hash.new 0) { |sum, e| sum[e.name] += e.apples_eaten;sum }
答案 2 :(得分:0)
具有多个属性:
# list the fruit
attributes = %i(apples_eaten pears_eaten)
# define the struct
Meal = Struct.new(:name, *attributes)
# create sample data
apple_array = [Meal.new("bob", 1, 2), Meal.new("marge", 5, 4), Meal.new("gomer", 7, 1), Meal.new("bob", 5, 0), Meal.new("gomer", 3, 9)]
totals = apple_array.inject(
# start from a hash that defaults to empty stomach
Hash.new { |h, k| h[k] = Meal.new(k, 0, 0) }
) { |r, e|
# then for each value in apple_array, add each attribute to the total
attributes.each do |a|
r[e.name][a] += e[a]
end
# mustn't forget to return the accumulator
r
# hash was just for easy lookup, but we want an array, so...
}.values
require 'pp'
pp totals
# => [#<struct Meal name="bob", apples_eaten=6, pears_eaten=2>,
# #<struct Meal name="marge", apples_eaten=5, pears_eaten=4>,
# #<struct Meal name="gomer", apples_eaten=10, pears_eaten=10>]
答案 3 :(得分:0)
这是我的解决方案。它按:name
对数组进行排序,然后将数组划分为子数组,其中每个项具有相同的:name
,然后将子数组减少为单个项,其总和为:apples_eaten
。显而易见,如何总结其他属性......
apple_array = apple_array
.sort_by { |a| a[:name] }
.slice_when { |a, b| a[:name] != b[:name] }
.map do |l|
l.reduce do |a, b|
a[:apples_eaten] += b[:apples_eaten]
a
end
end