Python 中两个列表的异或
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22736641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XOR on two lists in Python
提问by
I'm a beginner in Python, and I have to do the XOR between two lists (the first one with the length : 600 and the other 60)
我是 Python 的初学者,我必须在两个列表之间进行 XOR(第一个长度为 600,另一个为 60)
I really don't know how to do that, if somebody can explain me how, it will be a pleasure.
我真的不知道该怎么做,如果有人能解释我怎么做,那将是一种乐趣。
I have to do that to find the BPSK signal module, and I'm wondering on how doing that with two lists that haven't the same length. I saw this post : Comparing two lists and only printing the differences? (XORing two lists)but the length of lists is the same
我必须这样做才能找到 BPSK 信号模块,我想知道如何使用两个长度不同的列表来做到这一点。我看到了这篇文章:比较两个列表并只打印差异?(异或两个列表)但列表的长度相同
Thanks for your help, and sry for my bad english Rom
感谢您的帮助,并为我糟糕的英语 Rom 感到抱歉
采纳答案by unutbu
Given sequences seq1
and seq2
, you can calculate the symmetric differencewith
鉴于序列seq1
和seq2
,可以计算出对称差与
set(seq1).symmetric_difference(seq2)
For example,
例如,
In [19]: set([1,2,5]).symmetric_difference([1,2,9,4,8,9])
Out[19]: {4, 5, 8, 9}
Tip: Generating the set with the smaller list is generally faster:
提示:生成具有较小列表的集合通常更快:
In [29]: %timeit set(range(60)).symmetric_difference(range(600))
10000 loops, best of 3: 25.7 μs per loop
In [30]: %timeit set(range(600)).symmetric_difference(range(60))
10000 loops, best of 3: 41.5 μs per loop
The reason why you may want to use symmetric difference
instead of ^
(despite the beauty of its syntax) is because the symmetric difference
method can take a list as input. ^
requires both inputs be sets. Converting bothlists into sets is a little more computation than is minimally required.
您可能想要使用symmetric difference
而不是的原因^
(尽管它的语法很美)是因为该symmetric difference
方法可以将列表作为输入。^
需要设置两个输入。将两个列表转换为集合比最低要求的计算要多一些。
This question has been marked of as a duplicate of this questionThat question, however, is seeking a solution to this problem without using sets.
这个问题已被标记为这个问题的重复 那个问题,但是,正在寻求不使用集合的解决方案。
The accepted solution,
公认的解决方案,
[a for a in list1+list2 if (a not in list1) or (a not in list2)]
is not the recommended way to XOR two lists if sets are allowed. For one thing, it's over 100 times slower:
如果允许集合,则不是对两个列表进行异或的推荐方法。一方面,它慢了 100 多倍:
In [93]: list1, list2 = range(600), range(60)
In [94]: %timeit [a for a in list1+list2 if (a not in list1) or (a not in list2)]
100 loops, best of 3: 3.35 ms per loop
回答by Maxime Lorant
There's the XOR
operator on set. Assuming you have no duplicates (and you don't care about checking if an element appears more than 1 times or not in the second list), you can use the ^
operator on set:
现场有XOR
接线员。假设您没有重复项(并且您不关心检查元素是否在第二个列表中出现超过 1 次),您可以^
在集合上使用运算符:
>>> set([1, 2, 3, 4, 5]) ^ set([1, 3, 4, 5, 6])
set([2, 6])
>>> set(range(80)) ^ set(range(60))
set([60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79])