在 Ruby 中比较数组并删除重复项?
比较多个数组并删除重复项的最简单方法是什么?
所以(在这种情况下是数组内的数组)...
a = [[2, 1], [3, 3], [7, 2], [5, 6]]
b = [[2, 1], [6, 7], [9, 9], [4, 3]]
c = [[2, 1], [1, 1], [2, 2], [9, 9]]
d = [[2, 1], [9, 9], [2, 2], [3, 1]]
...将会出现(优先级为数组 a,然后是 b,然后是 c,然后是 d)
a = [[2, 1], [3, 3], [7, 2], [5, 6]]
b = [[6, 7], [9, 9], [4, 3]]
c = [[1, 1], [2, 2]]
d = [[3, 1]]
What would be the easiest way to compare multiple arrays, and remove duplicates?
So (arrays inside arrays in this case)...
a = [[2, 1], [3, 3], [7, 2], [5, 6]]
b = [[2, 1], [6, 7], [9, 9], [4, 3]]
c = [[2, 1], [1, 1], [2, 2], [9, 9]]
d = [[2, 1], [9, 9], [2, 2], [3, 1]]
...would come out (with priority given to array a, then b, then c, then d)
a = [[2, 1], [3, 3], [7, 2], [5, 6]]
b = [[6, 7], [9, 9], [4, 3]]
c = [[1, 1], [2, 2]]
d = [[3, 1]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它只是设置差或减法,你可以这样写。运算符重载可以是一种幸福:)
a
就是这样。It's just set difference or subtraction and you can write it as such. Operator overloading can be a bliss :)
a
is what it is.将所有数组放在一个大数组中:
您可以像这样实现您想要的:
注意:我不确定从哪个 Ruby 版本
Array#flatten
开始接受参数。编辑:这是 Anurag 的想法,应用于注入:
Having all the arrays in one big array:
You can achieve what you want like this:
Note: I am not sure from which Ruby version
Array#flatten
started accepting arguments.Edit: Here's Anurag's idea, applied to inject: