gpt4 book ai didi

ruby-on-rails - Rails 散列按它们的值组合

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

我有一个问题列表,我需要将它们分开。关系是

Question_set has_many questions
BookVolume has_many questions
Subject has_many book_volumes
Publisher has_many subjects
Section has_many :questions

现在我只把questions 和它们的相关模型id, name 放到 里面的hash 中数组

data = []
question_set.questions.each do |q|
data << {publisher: {id: q.publisher.id, name: q.publisher.name}, subject: {id: q.book_volume.subject.id, name: q.book_volume.subject.name}, volume: {id: q.book_volume_id, name: q.book_volume.name}, chapter: [{id: q.section_id, name: q.section.name}]}
end

因此,data基本上就是

>>data

[
{
:publisher => {
:id => 96,
:name => "P1"
},
:subject => {
:id => 233,
:name => "S1"
},
:volume => {
:id => 1136,
:name => "V1"
},
:chapter => [
{
:id => 16155,
:name => "C1"
}
]
},
{
:publisher => {
:id => 96,
:name => "P1"
},
:subject => {
:id => 233,
:name => "S1"
},
:volume => {
:id => 1136,
:name => "V1"
},
:chapter => [
{
:id => 16158,
:name => "C2"
}
]
}
]

但是,如果 chapter 具有相同的 publishersubjectvolume,我希望合并它们所以,在这种情况下,它将是

>>data 

[
{
:publisher => {
:id => 96,
:name => "P1"
},
:subject => {
:id => 233,
:name => "S1"
},
:volume => {
:id => 1136,
:name => "V1"
},
:chapter => [
{
:id => 16155,
:name => "C2"
},
{
:id => 16158,
:name => "C2"
}
]
}
]

最佳答案

代码

def group_em(data)
data.group_by { |h| [h[:publisher], h[:subject], h[:volume]] }.
map do |k,v|
h = { publisher: k[0], subject: k[1], volume: k[2] }
h.update(chapters: v.each_with_object([]) { |f,a|
a << f[:chapter] }.flatten)
end
end

示例

data 等于散列数组(上面的第一个数组)。

group_em(data)
#=> [{:publisher=>{:id=>96, :name=>"P1"},
# :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"},
# :chapters=>[{:id=>16155, :name=>"C1"}, {:id=>16158, :name=>"C2"}]
# }
# ]

此处 data 仅包含两个散列,并且这些散列对于键 :publisher:subject 具有相同的值:音量。此代码允许数组具有任意数量的散列,并将通过这三个键的值的数组对它们进行分组,为这些组中的每一个生成一个散列。此外,键 :chapters 的值是包含单个散列的数组,但此代码允许该数组包含多个散列。 (如果该数组总是只有一个散列,请考虑将 :chapters 的值设为散列本身,而不是包含该散列的数组。)

解释

参见 Enumerable#group_byHash#update (又名 Hash#merge!)。

步骤如下。

h = data.group_by { |h| [h[:publisher], h[:subject], h[:volume]] }
#=> {
# [{:id=>96, :name=>"P1"},
# {:id=>233, :name=>"S1"},
# {:id=>1136, :name=>"V1"}
# ]=>[{:publisher=>{:id=>96, :name=>"P1"},
# :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"},
# :chapter=>[{:id=>16155, :name=>"C1"}]
# },
# {:publisher=>{:id=>96, :name=>"P1"},
# :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"},
# :chapter=>[{:id=>16158, :name=>"C2"}]
# }
# ]
# }

第一个键值对传递给 map 的 block ,并分配 block 变量。

k,v = h.first
#=> [[{:id=>96, :name=>"P1"}, {:id=>233, :name=>"S1"}, {:id=>1136, :name=>"V1"}],
# [{:publisher=>{:id=>96, :name=>"P1"}, :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"}, :chapter=>[{:id=>16155, :name=>"C1"}]},
# {:publisher=>{:id=>96, :name=>"P1"}, :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"}, :chapter=>[{:id=>16158, :name=>"C2"}]}]]
k #=> [{:id=>96, :name=>"P1"}, {:id=>233, :name=>"S1"}, {:id=>1136, :name=>"V1"}]
v #=> [{:publisher=>{:id=>96, :name=>"P1"},
# :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"},
# :chapter=>[{:id=>16155, :name=>"C1"}]},
# {:publisher=>{:id=>96, :name=>"P1"},
# :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"},
# :chapter=>[{:id=>16158, :name=>"C2"}]}]

并进行分块计算。

h = { publisher: k[0], subject: k[1], volume: k[2] }
#=> {:publisher=>{:id=>96, :name=>"P1"},
# :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"}
# }
a = v.each_with_object([]) { |f,a| a << f[:chapter] }
#=> [[{:id=>16155, :name=>"C1"}], [{:id=>16158, :name=>"C2"}]]
b = a.flatten
#=> [{:id=>16155, :name=>"C1"}, {:id=>16158, :name=>"C2"}]
h.update(chapters: b)
#=> {:publisher=>{:id=>96, :name=>"P1"},
# :subject=>{:id=>233, :name=>"S1"},
# :volume=>{:id=>1136, :name=>"V1"},
# :chapters=>[{:id=>16155, :name=>"C1"}, {:id=>16158, :name=>"C2"}]
# }

Hash#merge可以用来代替 Hash#update

关于ruby-on-rails - Rails 散列按它们的值组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39360885/

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