简体   繁体   English

Python 如何对集合列表进行排序?

[英]How does Python sort a list of sets?

Python sorts lists of tuples by looking at the elements of the tuples, in order. Python 通过按顺序查看元组的元素来对元组列表进行排序。 Since sets are unordered, how does Python sort a list of sets?由于集合是无序的,Python 是如何对集合列表进行排序的?

Edit: The question and accepted answer in this post are more general and the document given is very in-depth.编辑: 这篇文章的问题和接受的答案更笼统,给出的文件非常深入。 My question is not a duplicate.我的问题不是重复的。

Regardless of what's in a list, the elements' __lt__ methods are the only comparison methods consulted.不管列表中有什么,元素的__lt__方法是唯一参考的比较方法。 For sets, a < b means " a is a proper subset of b ", which isn't enough to define a total order.对于集合, a < b表示“ ab子集”,这不足以定义全序。 That's why the result is, in general, undefined.这就是为什么结果通常是未定义的。 It may be any permutation of the original list consistent with which pairs of list elements the implementation happens to apply __lt__ to.它可以是原始列表的任何排列,与实现恰好将__lt__应用到的列表元素对一致。

If, for every pair of sets in the list, one is in fact a proper subset of the other, then the list will be sorted from smallest (cardinality) set to largest.如果对于列表中的每一对集合,一个实际上是另一个集合的真子集,那么列表将从最小(基数)集合到最大排序。 Otherwise little can be said.否则就无话可说了。 For example:例如:

>>> sorted([{5, 6}, {3, 4}, {5}, {3}])  # nothing changes
[{5, 6}, {3, 4}, {5}, {3}]

What happens is a consequence of undefined implementation details.发生的事情是未定义的实现细节的结果。 Since I wrote list.sort() , I know what happens in this case, but it's not guaranteed to always work this way:自从我写了list.sort() ,我知道在这种情况下会发生什么,但不能保证总是这样工作:

First the implementation asks "is {3, 4} < {5, 6} ?".首先,实现会询问“是{3, 4} < {5, 6}吗?”。 No, so the order of the first two elements is consistent with being sorted already.不,所以前两个元素的顺序与已经排序是一致的。 It next asks "is {5} < {3, 4} ?".它接下来询问“是{5} < {3, 4}吗?”。 No, so the first three elements appear to be already sorted.不,所以前三个元素似乎已经排序。 Finally it asks "is {3} < {5} ?".最后它问“是{3} < {5}吗?”。 No again, so the original list's entire order is consistent with being already sorted, and nothing changes.再次没有,所以原始列表的整个顺序与已经排序的一致,没有任何变化。

A future implementation may, eg, ask "is {5} < {5, 6} ?"例如,未来的实现可能会问“是{5} < {5, 6}吗?” at some point, and since "yes" decide {5} needs to appear before {5, 6} .在某些时候,因为“是”决定{5}需要出现在{5, 6} So the result is simply not defined.所以结果根本没有定义。

Sets are partially ordered, so集合是部分有序的,所以

the output of the list.sort() method is undefined for lists of sets. list.sort() 方法的输出对于集合列表是未定义的。

https://docs.python.org/3/library/stdtypes.html#set https://docs.python.org/3/library/stdtypes.html#set

The __le__ operator on sets define the partial ordering " subset ". __le__上的__le__运算符定义了偏序“子集”。 Therefore the sorted order is undefined.因此排序顺序是未定义的。

{3} < {5} is false, but so is {5} < {3} so the sort algorithm will usually not rearrange them. {3} < {5}是假的,但{5} < {3} {3} < {5}也是假的,因此排序算法通常不会重新排列它们。

Citation from Python3 documentaton about sets :来自Python3 文档中关于集合的引用:

The subset and equality comparisons do not generalize to a total ordering function.子集和相等比较不能推广到总排序函数。 For example, any two nonempty disjoint sets are not equal and are not subsets of each other, so all of the following return False: a<b, a==b, or a>b .例如,任何两个非空不相交集不相等,也不是彼此的子集,因此以下所有返回 False: a<b, a==b, or a>b

Since sets only define partial ordering (subset relationships), the output of the list.sort() method is undefined for lists of sets.由于集合仅定义部分排序(子集关系),因此 list.sort() 方法的输出对于集合列表是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM