两个列表中公共值的方法
当我有 2 个数组(列表)并且我想获取一个仅包含两个数组共有的值的数组(列表)时,Ruby 是否有一个可以使用的方法?像这样......
a = [1,2,3]
b = [3,4,5]
=> the method would return [3]
反之亦然,这些数组(列表)中的值是“唯一的”。
a = [1,2,3]
b = [3,4,5]
=> the method would return [1,2,4,5]
Does Ruby has a method I could use when I have 2 arrays (lists) and I want to get an array (list) of only the values common to both arrays? Like this..
a = [1,2,3]
b = [3,4,5]
=> the method would return [3]
And the other way around, values that are "unique" in those arrays (lists).
a = [1,2,3]
b = [3,4,5]
=> the method would return [1,2,4,5]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ruby 中没有数组的 XOR 方法,因此您可以通过其他方法来完成。这里有2种方法:
There are no XOR method for arrays in Ruby, so you may do it via another methods. Here are 2 ways:
您要查找的词是交集和对称差。 AFAIK 在 Ruby 中是这样的:
The words you are looking for are intersection and symmetric difference. AFAIK it's this in Ruby: