当我有两个数组(列表),并且我想获取一个只包含两个数组共有的值的数组(列表)时,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]发布于 2011-01-12 01:50:40
AND : a & bRuby中没有用于数组的XOR方法,因此您可以通过其他方法来实现。这里有两种方式:
XOR : (a | b) - (a & b)
XOR : (a + b) - (a & b) # this result can have duplicates!
XOR : (a - b) | (b - a)
XOR : (a - b) + (b - a) # this result can have duplicates!发布于 2011-01-12 00:41:40
您要查找的单词是对称交集和对称差分。在Ruby中是这样的:
[1,2,3] & [3,4,5] = [3]
[1,2,3] ^ [3,4,5] = [1,2,4,5]https://stackoverflow.com/questions/4660061
复制相似问题