gpt4 book ai didi

ruby - 将散列值搜索到另一个散列中并创建具有匹配值的新散列

转载 作者:行者123 更新时间:2023-12-05 09:35:36 25 4
gpt4 key购买 nike

在一个 Ruby 项目中,我有以下两个散列(来自两个模型):“用户”和“冲突”。

我想找到迭代它们的方法,以便在用户的值中搜索冲突的值,并获得一个散列,对于每个冲突,用户将包含其所有值:

users = {
'user1' => ['resp1', 'resp2', 'resp3', 'resp4'],
'user2' => ['resp1', 'resp2', 'resp3', 'resp4'],
'user3' => ['resp1', 'resp4']
}

conflicts = {
'c1'=> ['resp1', 'resp2'], 'c2'=> ['resp1', 'resp3'],
'c3'=> ['resp1', 'resp4'], 'c4'=> ['resp3', 'resp5','resp6']
}

期望的结果:

conflict_users = {
'c1'=> ['user1', 'user2'], 'c2'=> ['user1', 'user2'],
'c3'=> ['user1', 'user2','user3'],'c4'=> ['nill']
}

-- 关于如何实现这一点有什么想法吗?

最佳答案

可以通过如下方式得到想要的返回值。

conflicts.transform_values do |c_resps|
users.each_with_object([]) do |(user,user_resps),c_users|
c_users << user if (c_resps & user_resps) == c_resps
end.tap { |c_users| c_users.replace(['nill']) if c_users.empty? }
end
#=> {"c1"=>["user1", "user2"], "c2"=>["user1", "user2"],
# "c3"=>["user1", "user2", "user3"], "c4"=>["nill"]}

参见 Hash#transform_values , Array#&#tap .

(c_resps & user_resps)

c_users << user if (c_resps & user_resps) == c_resps

是一个包含c_resps中所有元素的数组(conflicts 中的值)也在 user_resps 中(users 中的值)。因此 c_resps 中的所有元素包含在 user_resps 中当且仅当 (c_resps & user_resps) == c_resps 1.

请注意,离开 "c4"=>["nill"] 会更容易(并且更像 Ruby)作为"c4"=>[] , 在这种情况下 .tap { |c_users| c_users.replace(['nill']) if arr1.empty? }可以删除。

为了帮助您了解发生了什么,我稍微重新组织了方法并插入了 puts语句在方法运行时显示不同对象的值。

conflicts.transform_values do |c_resps|
puts "\nc_resps = #{c_resps}"
c_users = []
users.each do |user,user_resps|
puts " user=#{user}, user_resps=#{user_resps}"
puts " c_resps & user_resps = #{c_resps & user_resps}"
c_users << user if (c_resps & user_resps) == c_resps
puts " c_users after c_users << user if... = #{c_users}"
end
c_users = ['nill'] if c_users.empty?
puts "c_users after c_users = ['nill'] if c_users.empty? = #{c_users}"
c_users
end #=> {"c1"=>["user1", "user2"], "c2"=>["user1", "user2"],
# "c3"=>["user1", "user2", "user3"], "c4"=>["nill"]}

这会显示以下内容。

c_resps = ["resp1", "resp2"]
user=user1, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp1", "resp2"]
c_users after c_users << user if... = ["user1"]
user=user2, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp1", "resp2"]
c_users after c_users << user if... = ["user1", "user2"]
user=user3, user_resps=["resp1", "resp4"]
c_resps & user_resps = ["resp1"]
c_users after c_users << user if... = ["user1", "user2"]
c_users after c_users = ['nill'] if c_users.empty? = ["user1", "user2"]
c_resps = ["resp1", "resp3"]
user=user1, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp1", "resp3"]
c_users after c_users << user if... = ["user1"]
user=user2, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp1", "resp3"]
c_users after c_users << user if... = ["user1", "user2"]
user=user3, user_resps=["resp1", "resp4"]
c_resps & user_resps = ["resp1"]
c_users after c_users << user if... = ["user1", "user2"]
c_users after c_users = ['nill'] if c_users.empty? = ["user1", "user2"]
c_resps = ["resp1", "resp4"]
user=user1, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp1", "resp4"]
c_users after c_users << user if... = ["user1"]
user=user2, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp1", "resp4"]
c_users after c_users << user if... = ["user1", "user2"]
user=user3, user_resps=["resp1", "resp4"]
c_resps & user_resps = ["resp1", "resp4"]
c_users after c_users << user if... = ["user1", "user2", "user3"]
c_users after c_users = ['nill'] if c_users.empty? = ["user1", "user2", "user3"]
c_resps = ["resp3", "resp5", "resp6"]
user=user1, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp3"]
c_users after c_users << user if... = []
user=user2, user_resps=["resp1", "resp2", "resp3", "resp4"]
c_resps & user_resps = ["resp3"]
c_users after c_users << user if... = []
user=user3, user_resps=["resp1", "resp4"]
c_resps & user_resps = []
c_users after c_users << user if... = []
c_users after c_users = ['nill'] if c_users.empty? = ["nill"]

1。 (c_resps & v) == c_resps可以替换为(以及其他)(v-c_resps).empty?arr1 << k if v.all? { |e| c_resps.include?(e) } .

关于ruby - 将散列值搜索到另一个散列中并创建具有匹配值的新散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65763409/

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