如何检查Ruby数组是否包含多个值之一?

问题描述:

我有两个Ruby数组,我需要看他们是否有任何共同的值。我可以循环遍历一个数组中的每个值,并在另一个数组中包含?(),但我确定有更好的方法。它是什么? (阵列都保存字符串。)如何检查Ruby数组是否包含多个值之一?

谢谢。

+0

你关心它的共同点是什么元素呢? – Levi 2010-04-09 00:19:02

+0

不是。我想知道的是,如果两者有共同点的话。 – Colen 2010-04-13 20:51:09

Set intersect他们:

a1 & a2 

下面是一个例子:

> a1 = [ 'foo', 'bar' ] 
> a2 = [ 'bar', 'baz' ] 
> a1 & a2 
=> ["bar"] 
> !(a1 & a2).empty? # Returns true if there are any elements in common 
=> true 
+3

好吧,OP想要“检查”,所以布尔结果会更合适:!(a1&a2).empty? – tokland 2011-11-06 10:39:25

+4

我会去(a1&a2).any?而不是!(a1&a2).empty? – rilla 2014-08-27 09:10:38

+1

@rilla'any?'在这种情况下有效,但在处理'false'和'nil'值时不起作用:'[nil,false] .any? #=> false'。 – Stefan 2014-10-31 10:33:54

任何公共价值?如果您正在寻找但一个完整的路口(含重复)&

[ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ] 

问题是比较复杂的已经有一个堆栈溢出这里:你可以使用相交的操作How to return a Ruby array intersection with duplicate elements? (problem with bigrams in Dice Coefficient)

或快速snippet其定义“real_intersection”并验证以下测试

class ArrayIntersectionTests < Test::Unit::TestCase  
    def test_real_array_intersection 
    assert_equal [2], [2, 2, 2, 3, 7, 13, 49] & [2, 2, 2, 5, 11, 107] 
    assert_equal [2, 2, 2], [2, 2, 2, 3, 7, 13, 49].real_intersection([2, 2, 2, 5, 11, 107]) 
    assert_equal ['a', 'c'], ['a', 'b', 'a', 'c'] & ['a', 'c', 'a', 'd'] 
    assert_equal ['a', 'a', 'c'], ['a', 'b', 'a', 'c'].real_intersection(['a', 'c', 'a', 'd']) 
    end 
end 

使用交点看起来不错,但效率不高。我会用“任何?”在第一个数组上(以便在第二个数组中找到其中一个元素时停止迭代)。此外,在第二个阵列上使用Set可以快速进行会员资格检查。即:

a = [:a, :b, :c, :d] 
b = Set.new([:c, :d, :e, :f]) 
c = [:a, :b, :g, :h] 

# Do a and b have at least a common value? 
a.any? {|item| b.include? item} 
# true 

# Do c and b have at least a common value? 
c.any? {|item| b.include? item} 
#false 
+1

根据简单值与对象属性比较,基准测试显示此速度比(更美观令人满意的)集交集方法快1.5-2倍。正如上面的评论中所建议的那样,使用'any?'而不是'empty?'设置交叉点,稍有不同,但并未改变结果。 (严格考虑表现,以及自第一场比赛以来任何'保释'的预期。) – 2016-04-13 14:54:14

试试这个

a1 = [ 'foo', 'bar' ] 
a2 = [ 'bar', 'baz' ] 
a1-a2 != a1 
true