是否有一个 Ruby 函数可以对两组字符串执行异或运算?

发布于 2024-10-03 00:24:00 字数 274 浏览 1 评论 0原文

我有两个字符串数组,我想找到不在两者交集中的字符串集。我想要的是 MATLAB 中 SETXOR 的等价物: http://www.mathworks.com/help/techdoc/ref/setxor.html

我将术语集与 Array 互换使用。

当然,我可以在形成这个问题的时间内轻松地写出自己的问题,但我认为我应该问。

I have two Arrays of strings, and I would like to find the set of strings not in the intersection of both. The equivalent of SETXOR in MATLAB is what I want:
http://www.mathworks.com/help/techdoc/ref/setxor.html

I'm using the term set interchangeably with Array.

Of course, I could have just as easily written my own in the time taken to form this question, but I thought I should ask.

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

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

发布评论

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

评论(3

丑丑阿 2024-10-10 00:24:00
array1 + array2 - (array1 & array2)

它比写一个问题更短......

顺便说一下,R​​uby 有一个类 Set ,所以最好不要使用这个词作为数组的同义词。

array1 + array2 - (array1 & array2)

It was shorter, than to write a question...

By the way, Ruby has a class Set, so better not to use this word as a synonym to an Array.

面犯桃花 2024-10-10 00:24:00

是的,正如纳基隆所说,赛特。

require 'set'
s = Set.new('a'..'f')
a = ['f','d','e','e','h','i'] #or any enum
p s ^ a  #=> #<Set: {"h", "i", "a", "b", "c"}>

Yes, as Nakilon says, Set.

require 'set'
s = Set.new('a'..'f')
a = ['f','d','e','e','h','i'] #or any enum
p s ^ a  #=> #<Set: {"h", "i", "a", "b", "c"}>
别念他 2024-10-10 00:24:00

你总是可以做

(array0 - array1) + (array1 - array0)

a = [1, 2, 3, 4, 5]
b = [2, 5, 8]
(a - b) + (b - a)
  # => [1, 3, 4, 8]

You can always just do

(array0 - array1) + (array1 - array0)

a = [1, 2, 3, 4, 5]
b = [2, 5, 8]
(a - b) + (b - a)
  # => [1, 3, 4, 8]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文