Ruby在数组中基于键合并哈希,哈希中另一个键的和值
我有一个来自dynamo表的散列数组,我需要按一个键对其进行分组,并对另一个键的值求和。我的阵列看起来类似于:Ruby在数组中基于键合并哈希,哈希中另一个键的和值,ruby,optimization,hash,amazon-dynamodb,Ruby,Optimization,Hash,Amazon Dynamodb,我有一个来自dynamo表的散列数组,我需要按一个键对其进行分组,并对另一个键的值求和。我的阵列看起来类似于: data = [ { 'state' => 'Florida', 'minutes_of_sun' => 10, 'timestamp' => 1497531600, 'region' => 'Southeast' }, { 'state' => 'Florida', 'minutes_of_sun' => 7, 'timestamp' =&
data = [
{ 'state' => 'Florida', 'minutes_of_sun' => 10, 'timestamp' => 1497531600, 'region' => 'Southeast' },
{ 'state' => 'Florida', 'minutes_of_sun' => 7, 'timestamp' => 1497531600, 'region' => 'Southeast' },
{ 'state' => 'Florida', 'minutes_of_sun' => 2, 'timestamp' => 1497531600, 'region' => 'Southeast' },
{ 'state' => 'Georgia', 'minutes_of_sun' => 15, 'timestamp' => 1497531600, 'region' => 'Southeast' },
{ 'state' => 'Georgia', 'minutes_of_sun' => 5, 'timestamp' => 1497531600, 'region' => 'Southeast' }
]
我希望的最终结果是:
data = [
{ 'state' => 'Florida', 'minutes_of_sun' => 19, 'region' => 'Southeast' },
{ 'state' => 'Georgia', 'minutes_of_sun' => 20, 'region' => 'Southeast' }
]
我已经能够通过我在下面写的一种方法做到这一点,但它既慢又笨重。我想知道是否有一种更快/更少的方法来实现这一点
def combine_data(data)
combined_data = []
data.each do |row|
existing_data = combined_data.find { |key| key['state'] == row['state'] }
if existing_data.present?
existing_data['minutes_of_sun'] += row['minutes_of_sun']
else
combined_data << row
end
end
combined_data
end
def组合_数据(数据)
组合_数据=[]
数据。每个do |行|
现有_数据=组合_数据。查找{|键|键['state']==行['state']}
如果现有_数据存在?
现有_数据['minutes_of_sun']+=行['minutes_of_sun']
其他的
组合数据试试这个
data.group_by { |item| item['state'] }.values.map do |arr|
h = arr.first
h.delete('timestamp')
h.merge('minutes_of_sun' => arr.inject { |acc, h| acc + h['minutes_of_sun'] })
end
=> [{"state"=>"Florida", "minutes_of_sun"=>19, "region"=>"Southeast"}, {"state"=>"Georgia", "minutes_of_sun"=>20, "region"=>"Southeast"}]
来自ruby 2.4.0
data.group_by { |item| item['state'] }.values.map do |arr|
h = arr.first
h.delete('timestamp')
h.merge('minutes_of_sun' => arr.sum { |item| item['minutes_of_sun'] })
end
=> [{"state"=>"Florida", "minutes_of_sun"=>19, "region"=>"Southeast"}, {"state"=>"Georgia", "minutes_of_sun"=>20, "region"=>"Southeast"}]
您可以使用(akamerge!
)的形式,它使用一个块来确定合并的两个哈希中存在的键的值。有关该块中三个块变量的说明,请参见文档
data = [
{ 'state'=>'Florida', 'sun_min'=>10, 'stamp'=>149, 'region'=>'SE' },
{ 'state'=>'Georgia', 'sun_min'=>15, 'stamp'=>149, 'region'=>'SE' },
{ 'state'=>'Georgia', 'sun_min'=> 5, 'stamp'=>149, 'region'=>'SE' }
]
data.each_with_object({}) do |g,h|
h.update(g['state']=>g.reject { |k,_| k=='stamp' }) do |_,o,n|
o.merge('sun_min'=>o['sun_min']+n['sun_min'])
end
end.values
#=> [{"state"=>"Florida", "sun_min"=>10, "region"=>"SE"},
# {"state"=>"Georgia", "sun_min"=>20, "region"=>"SE"}]
请注意,如果没有.values
,则返回
#=> {"Florida"=>{"state"=>"Florida", "sun_min"=>10, "region"=>"SE"},
# "Georgia"=>{"state"=>"Georgia", "sun_min"=>20, "region"=>"SE"}}
[代码>数据.数据.数据.集团公司(U U)由{h | h | h | h[国家]h[国家]h[国家]}.数值.地图.地图{hs | h | h | h | h[国家]国家.地图{hs.注射{a,b,b 124;a.合并(b)合并(b)合并(b)合并(b){{{{{b)键,键,键,键,键,老瓦尔,老瓦尔,旧瓦尔,旧瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,新瓦尔,旧瓦尔,旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔旧瓦尔如果您不介意原始的数据
哈希修改)这个问题可能重复被问了很多次并回答了很多次(你是先尝试搜索的吗?)