如何检查Ruby数组是否包含几个值中的一个?

时间:2021-08-29 08:36:49

I have two Ruby arrays, and I need to see if they have any values in common. I could just loop through each of the values in one array and do include?() on the other, but I'm sure there's a better way. What is it? (The arrays both hold strings.)

我有两个Ruby数组,我需要看看它们是否有共同的值。我可以循环遍历一个数组中的每个值,并在另一个数组中包含?(),但我肯定有更好的方法。它是什么?(数组都包含字符串。)

Thanks.

谢谢。

4 个解决方案

#1


70  

Set intersect them:

集相交:

a1 & a2

Here's an example:

这里有一个例子:

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

#2


7  

Any value in common ? you can use the intersection operator : &

有什么共同的价值吗?你可以使用交集运算符:&

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

If you are looking for a full intersection however (with duplicates) the problem is more complex there is already a stack overflow here : How to return a Ruby array intersection with duplicate elements? (problem with bigrams in Dice Coefficient)

如果您正在寻找一个完整的交集,但是(使用重复的)问题更复杂,这里已经有一个堆栈溢出:如何返回一个带有重复元素的Ruby数组交集?(骰子系数的双图问题)

Or a quick snippet which defines "real_intersection" and validates the following test

或一个定义“real_crossroads”并验证以下测试的快速代码片段

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

#3


3  

Using intersection looks nice, but it is inefficient. I would use "any?" on the first array (so that iteration stops when one of the elements is found in the second array). Also, using a Set on the second array will make membership checks fast. i.e.:

使用交点看起来不错,但效率很低。我将在第一个数组中使用“any?”(以便当第二个数组中发现一个元素时,迭代停止)。此外,在第二个数组上使用集合将使成员资格检查快速。例如:

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

#4


0  

Try this

试试这个

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

#1


70  

Set intersect them:

集相交:

a1 & a2

Here's an example:

这里有一个例子:

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

#2


7  

Any value in common ? you can use the intersection operator : &

有什么共同的价值吗?你可以使用交集运算符:&

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

If you are looking for a full intersection however (with duplicates) the problem is more complex there is already a stack overflow here : How to return a Ruby array intersection with duplicate elements? (problem with bigrams in Dice Coefficient)

如果您正在寻找一个完整的交集,但是(使用重复的)问题更复杂,这里已经有一个堆栈溢出:如何返回一个带有重复元素的Ruby数组交集?(骰子系数的双图问题)

Or a quick snippet which defines "real_intersection" and validates the following test

或一个定义“real_crossroads”并验证以下测试的快速代码片段

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

#3


3  

Using intersection looks nice, but it is inefficient. I would use "any?" on the first array (so that iteration stops when one of the elements is found in the second array). Also, using a Set on the second array will make membership checks fast. i.e.:

使用交点看起来不错,但效率很低。我将在第一个数组中使用“any?”(以便当第二个数组中发现一个元素时,迭代停止)。此外,在第二个数组上使用集合将使成员资格检查快速。例如:

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

#4


0  

Try this

试试这个

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