确定 2 个列表是否具有相同的元素,无论顺序如何?
抱歉这个简单的问题,但我很难找到答案。
当我比较两个列表时,我想知道它们是否“相等”,因为它们具有相同的内容,但顺序不同。
例如:
x = ['a', 'b']
y = ['b', 'a']
我希望 x == y 的计算结果为 True。
Sorry for the simple question, but I'm having a hard time finding the answer.
When I compare 2 lists, I want to know if they are "equal" in that they have the same contents, but in different order.
Ex:
x = ['a', 'b']
y = ['b', 'a']
I want x == y
to evaluate to True
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以简单地检查包含 x 和 y 元素的多重集是否相等:
这要求元素是可哈希的;运行时间将为
O(n)
,其中n
是列表的大小。如果元素也是唯一的,您还可以转换为集合(相同的渐近运行时,在实践中可能会快一点):
如果元素不可散列,但可排序,则另一种选择(运行时为 O(n log n))
如果元素既不可散列也不可排序,您可以使用以下辅助函数。请注意,它会非常慢(
O(n²)
),并且通常应该不在不可散列和不可排序元素的深奥情况之外使用。You can simply check whether the multisets with the elements of x and y are equal:
This requires the elements to be hashable; runtime will be in
O(n)
, wheren
is the size of the lists.If the elements are also unique, you can also convert to sets (same asymptotic runtime, may be a little bit faster in practice):
If the elements are not hashable, but sortable, another alternative (runtime in
O(n log n)
) isIf the elements are neither hashable nor sortable you can use the following helper function. Note that it will be quite slow (
O(n²)
) and should generally not be used outside of the esoteric case of unhashable and unsortable elements.从您的示例中推断:
列表的元素不会重复(它们是唯一的)以及可散列(哪些字符串和其他元素)某些不可变的 Python 对象是),最直接且计算效率最高的答案使用 Python 的内置集合(语义上类似于您可能在学校学到的数学集合)。
在元素可散列但不唯一的情况下,
collections.Counter
在语义上也可以作为多重集工作,但它要慢得多:更喜欢使用
sorted
:如果元素是可排序的。这将考虑到非唯一或不可散列的情况,但这可能比使用集合慢得多。
实证实验
一项实证实验得出的结论是,人们应该更喜欢
set
,然后sorted
。仅当您需要其他内容(例如计数或作为多重集进一步使用)时,才选择Counter
。第一个设置:
和测试:
所以我们看到比较集合是最快的解决方案,比较排序列表是第二快的。
Inferring from your example:
that the elements of the lists won't be repeated (they are unique) as well as hashable (which strings and other certain immutable python objects are), the most direct and computationally efficient answer uses Python's builtin sets, (which are semantically like mathematical sets you may have learned about in school).
In the case that the elements are hashable, but non-unique, the
collections.Counter
also works semantically as a multiset, but it is far slower:Prefer to use
sorted
:if the elements are orderable. This would account for non-unique or non-hashable circumstances, but this could be much slower than using sets.
Empirical Experiment
An empirical experiment concludes that one should prefer
set
, thensorted
. Only opt forCounter
if you need other things like counts or further usage as a multiset.First setup:
And testing:
So we see that comparing sets is the fastest solution, and comparing sorted lists is second fastest.
这似乎有效,尽管对于大型列表来说可能很麻烦。
但是,如果每个列表必须包含其他列表的所有元素,那么上面的代码就会有问题。
当 len(A) != len(B) 时就会出现问题,在本例中,len(A) > len(A) != len(B) 时就会出现问题。长度(B)。为了避免这种情况,您可以再添加一条语句。
另一件事是,我在 Aaron Hall 在他的帖子中使用的相同条件下使用 timeit.repeat 对我的解决方案进行了基准测试。正如所怀疑的那样,结果令人失望。我的方法是最后一种。
set(x) == set(y)
是的。This seems to work, though possibly cumbersome for large lists.
However, if each list must contain all the elements of other then the above code is problematic.
The problem arises when
len(A) != len(B)
and, in this example,len(A) > len(B)
. To avoid this, you can add one more statement.One more thing, I benchmarked my solution with timeit.repeat, under the same conditions used by Aaron Hall in his post. As suspected, the results are disappointing. My method is the last one.
set(x) == set(y)
it is.正如上面评论中提到的,一般情况是很痛苦的。如果所有项目都是可散列的或所有项目都是可排序的,则相当容易。然而我最近不得不尝试解决一般情况。这是我的解决方案。我在发布后意识到这是与上面我在第一次通过时错过的解决方案的重复。无论如何,如果您使用切片而不是 list.remove() ,您可以比较不可变序列。
As mentioned in comments above, the general case is a pain. It is fairly easy if all items are hashable or all items are sortable. However I have recently had to try solve the general case. Here is my solution. I realised after posting that this is a duplicate to a solution above that I missed on the first pass. Anyway, if you use slices rather than list.remove() you can compare immutable sequences.