Arrays ruby比较不同数组中散列的值
我有两个不同的数组,每个数组由不同的哈希组成Arrays ruby比较不同数组中散列的值,arrays,ruby,hash,compare,Arrays,Ruby,Hash,Compare,我有两个不同的数组,每个数组由不同的哈希组成 new_array1 = [ {:index=>4, :column=>0, :ID=>"ABC"}, {:index=>4, :column=>1, :ID=>"XYZ"}, {:index=>4, :column=>2, :ID=>"BCD-1547"}
new_array1 = [
{:index=>4, :column=>0, :ID=>"ABC"},
{:index=>4, :column=>1, :ID=>"XYZ"},
{:index=>4, :column=>2, :ID=>"BCD-1547"}
]
new_array2 = [
{:index=>4, :column=>0, :ID=>"ABC"},
{:index=>4, :column=>1, :ID=>"IJK"},
{:index=>4, :column=>2, :ID=>"BCD-1547"}
]
仅当:index
和:column
值相同时,我想比较新数组1
中:ID
键的值与新数组2
中的值
ie)
if (new_array1[0][:index] == new_array2[0][:index]) and (new_array1[0][:column] == new_array2[0][:column])
if(new_array1[0][:ID] == new_array2[0][:ID])
# do something
end
end
有没有办法循环两个数组中的所有散列并找到匹配项?或者在ruby中用一种更优雅的方式来实现这一点
[new_array1, new_array2].map do |a|
a.group_by { |e| [e[:index], e[:column]] }
end.reduce do |f, l|
f.merge(l) { |_, f, l| [f.first[:ID], l.first[:ID]] }
end
# => {
# [ 4, 0 ] => [
# [0] "ABC",
# [1] "ABC"
# ],
# [ 4, 1 ] => [
# [0] "XYZ",
# [1] "IJK"
# ],
# [ 4, 2 ] => [
# [0] "BCD-1547",
# [1] "BCD-1547"
# ]
# }
在最后一个子句中放置
执行某些操作,除非f.first[:ID]==l.first[:ID]
而不是[f.first[:ID],l.first[:ID]]
,以执行任何您想要的操作。这将返回一个匹配哈希数组:
res = new_array1.inject([]) { |memo, hash| memo << hash if new_array2.any? { |hash2| hash[:ID] == hash2[:ID] && hash[:index] == hash2[:index] && hash[:column] == hash2[:column] }; memo }
# => [{:index=>4, :column=>0, :ID=>"ABC"}, {:index=>4, :column=>1, :ID=>"XYZ"}, {:index=>4, :column=>2, :ID=>"BCD-1547"}]
res.each do |hash|
# do something
end
inject
方法(别名,也称为reduce
)获取一个集合并从中创建一个新值,每次调用指定给inject
的块时,都会指定集合的下一个元素和上一个块的返回值(第一次调用块时,会将种子值传递给inject
)。这允许您建立类似于递归的值
这里有一些inject
的示例:
any?
方法将在给定块对任何给定集合元素返回true时立即返回true。如果块从未返回true,则any?
将返回false。因此:
[0,0,0,1,0].any? { |num| num == 1 } # => true
[0,0,0,0,0].any? { |num| num == 1 } # => false
如果所有散列都有相同的三个键,而没有其他键,那么
new_array1 & new_array2
#=> [{:index=>4, :column=>0, :ID=>"ABC"},
# {:index=>4, :column=>2, :ID=>"BCD-1547"}]
如果散列也可能有其他键,那么可以编写以下代码
new_array1 = [{:index=>4, :column=>0, :ID=>"ABC", :pet=>"cat"},
{:index=>4, :column=>1, :ID=>"XYZ", :bet=>"red"},
{:index=>4, :column=>2, :ID=>"BCD-1547", :met=>"Betty"}]
new_array2 = [{:index=>4, :column=>0, :ID=>"ABC", :tree=>"maple"},
{:index=>4, :column=>1, :ID=>"IJK", :colour=>"blue"},
{:index=>4, :column=>2, :ID=>"BCD-1547", :car=>"beemer"}]
keys = [:index,:column,:ID]
h1 = new_array1.each_with_object({}) { |g,h| h[g.select { |k,_| keys.include? k }] = g }
#=> {{:index=>4, :column=>0, :ID=>"ABC"}=>
# {:index=>4, :column=>0, :ID=>"ABC", :pet=>"cat"},
# {:index=>4, :column=>1, :ID=>"XYZ"}=>
# {:index=>4, :column=>1, :ID=>"XYZ", :bet=>"red"},
# {:index=>4, :column=>2, :ID=>"BCD-1547"}=>
# {:index=>4, :column=>2, :ID=>"BCD-1547", :met=>"Betty"}}
h2 = new_array2.each_with_object({}) { |g,h| h[g.select { |k,_| keys.include? k }] = g }
#=> {{:index=>4, :column=>0, :ID=>"ABC"}=>
# {:index=>4, :column=>0, :ID=>"ABC", :tree=>"maple"},
# {:index=>4, :column=>1, :ID=>"IJK"}=>
# {:index=>4, :column=>1, :ID=>"IJK", :colour=>"blue"},
# {:index=>4, :column=>2, :ID=>"BCD-1547"}=>
# {:index=>4, :column=>2, :ID=>"BCD-1547", :car=>"beemer"}}
(h1.keys & h2.keys).map { |k| [h1[k], h2[k]] }
#=> [[{:index=>4, :column=>0, :ID=>"ABC", :pet=>"cat"},
# {:index=>4, :column=>0, :ID=>"ABC", :tree=>"maple"}],
# [{:index=>4, :column=>2, :ID=>"BCD-1547", :met=>"Betty"},
# {:index=>4, :column=>2, :ID=>"BCD-1547", :car=>"beemer"}]]
1、考虑使用<代码> EACKY和Objult<代码> 2。这有助于“<代码>做某事< /代码>”?@ MuasasBWAW他们可以迭代<代码> RES>代码>做某事,我将更新答案。这正是我正在寻找的,谢谢!!你能解释一下<代码>注入[]吗?和
任何?
谢谢你,我在这里遇到了另一个问题,如果你能看一下的话,我将非常感谢你Cary。但是我很难理解什么类型的数据结构h1,h2
和(h1.keys和h2.keys)。映射{k |[h1[k],h2[k]}
是。看起来h1和h2是散列,而第三个是数组数组。你能验证一下吗?h1
和h2
确实是散列,它们的键也是散列。h1。键
和h2。因此键
是散列数组,它们的交点是这两个中键的散列数组>h1
和h2
。因此,我所做的是为new_array1
和new_array2
中的每个哈希创建一个包含感兴趣的三个键的哈希,并获得它们的交集,就像我在所有哈希都有相同的三个键而没有其他键的情况下所做的那样。最后一步是将该交集与h1一起使用e> 和h2
以获得所需的结果。谢谢,我在这里遇到了另一个问题,如果您能看一下,我将非常感谢,明天我会看一看。
new_array1 = [{:index=>4, :column=>0, :ID=>"ABC", :pet=>"cat"},
{:index=>4, :column=>1, :ID=>"XYZ", :bet=>"red"},
{:index=>4, :column=>2, :ID=>"BCD-1547", :met=>"Betty"}]
new_array2 = [{:index=>4, :column=>0, :ID=>"ABC", :tree=>"maple"},
{:index=>4, :column=>1, :ID=>"IJK", :colour=>"blue"},
{:index=>4, :column=>2, :ID=>"BCD-1547", :car=>"beemer"}]
keys = [:index,:column,:ID]
h1 = new_array1.each_with_object({}) { |g,h| h[g.select { |k,_| keys.include? k }] = g }
#=> {{:index=>4, :column=>0, :ID=>"ABC"}=>
# {:index=>4, :column=>0, :ID=>"ABC", :pet=>"cat"},
# {:index=>4, :column=>1, :ID=>"XYZ"}=>
# {:index=>4, :column=>1, :ID=>"XYZ", :bet=>"red"},
# {:index=>4, :column=>2, :ID=>"BCD-1547"}=>
# {:index=>4, :column=>2, :ID=>"BCD-1547", :met=>"Betty"}}
h2 = new_array2.each_with_object({}) { |g,h| h[g.select { |k,_| keys.include? k }] = g }
#=> {{:index=>4, :column=>0, :ID=>"ABC"}=>
# {:index=>4, :column=>0, :ID=>"ABC", :tree=>"maple"},
# {:index=>4, :column=>1, :ID=>"IJK"}=>
# {:index=>4, :column=>1, :ID=>"IJK", :colour=>"blue"},
# {:index=>4, :column=>2, :ID=>"BCD-1547"}=>
# {:index=>4, :column=>2, :ID=>"BCD-1547", :car=>"beemer"}}
(h1.keys & h2.keys).map { |k| [h1[k], h2[k]] }
#=> [[{:index=>4, :column=>0, :ID=>"ABC", :pet=>"cat"},
# {:index=>4, :column=>0, :ID=>"ABC", :tree=>"maple"}],
# [{:index=>4, :column=>2, :ID=>"BCD-1547", :met=>"Betty"},
# {:index=>4, :column=>2, :ID=>"BCD-1547", :car=>"beemer"}]]