如何检查一个多维 Ruby 数组中的元素是否存在于另一个多维 Ruby 数组中?

发布于 2024-07-30 11:27:22 字数 298 浏览 3 评论 0原文

我试图确定一个多维数组中的元素是否存在于另一个类似结构的数组中。

suspects = [['Rod', 100], ['Jane', 75], ['Freddy', 125]]
criminals = [['Bill', 75], ['Ted', 50], ['Rod', 75]]

我正在寻找的答案要么是真,要么是假。 在上面的示例中,响应将为 true,因为 Rod 存在于两个数组中。 第二层数组中的整数值没有意义。

我如何以典型的 Ruby 式简洁方式编写这样的测试代码?

I am trying to determine if the an element in one multi-dimensional array exists in another similarly structured array.

suspects = [['Rod', 100], ['Jane', 75], ['Freddy', 125]]
criminals = [['Bill', 75], ['Ted', 50], ['Rod', 75]]

The response I am looking for is either true or false. In the example above the response would be true because Rod exists in both arrays. The integer value has in the second tier array has no bearing.

How would I code such a test in typically Ruby-like succinctness?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傾旎 2024-08-06 11:27:22
suspects.any? do |suspect, _|
  criminals.any? do |criminal, _|
    suspect == criminal
  end
end
suspects.any? do |suspect, _|
  criminals.any? do |criminal, _|
    suspect == criminal
  end
end
网白 2024-08-06 11:27:22

像这样更快:

suspects.any? {|s,_| criminals.assoc(s)}

Faster like this:

suspects.any? {|s,_| criminals.assoc(s)}
没有心的人 2024-08-06 11:27:22

sepp2k 打败了我,但我必须提交只是为了表明我们在实施方面有多接近:


suspects.find do |suspect_name, _|
  criminals.find {|criminal_name, _| criminal_name == suspect_name}
 end 

我喜欢他使用any?,但认为内部块应该是内联的:)

sepp2k beat me to it, but i have to submit just to show how close we were in implementation:


suspects.find do |suspect_name, _|
  criminals.find {|criminal_name, _| criminal_name == suspect_name}
 end 

I like his use of any?, but think the inner block should be inline :)

長街聽風 2024-08-06 11:27:22

怎么样:

(suspect.size + criminal.size) > (suspect | criminals).size

示例:

suspects = [['Rod', 100], ['Jane', 75], ['Freddy', 125]]
criminals = [['Bill', 75], ['Ted', 50], ['Rod', 75]]

guilty = (suspects.size + criminals.size) > (suspects | criminals).size
# Returns false. Since no common element was found in the merging.

criminals << ['Jane', 75]
guilty = (suspects.size + criminals.size) > (suspects | criminals).size
# Returns true. Since one element is common, merged array will be shorter by one.

How about:

(suspect.size + criminal.size) > (suspect | criminals).size

Sample:

suspects = [['Rod', 100], ['Jane', 75], ['Freddy', 125]]
criminals = [['Bill', 75], ['Ted', 50], ['Rod', 75]]

guilty = (suspects.size + criminals.size) > (suspects | criminals).size
# Returns false. Since no common element was found in the merging.

criminals << ['Jane', 75]
guilty = (suspects.size + criminals.size) > (suspects | criminals).size
# Returns true. Since one element is common, merged array will be shorter by one.
送君千里 2024-08-06 11:27:22

我不一定推荐这个,但另一个单行选项(如果算上 require 则为两个)可能是这样的:

require 'set'
(suspects.map{|s| s[0]}.to_set & criminals.map{|c| c[0]}.to_set).size > 0

=> true

它从每个项目的第一个元素构建数组,然后将其转换为 设置Set&(相交)方法,我们查看结果的大小来得出答案。

I'm not necessarily recommending this, but anther one-line option (two if you count the require) could be this:

require 'set'
(suspects.map{|s| s[0]}.to_set & criminals.map{|c| c[0]}.to_set).size > 0

=> true

It builds arrays from the first element of each item then converts it to a Set. Set has the & (intersect) method and we look at the size of the result for our answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文