元组比较在 Python 中是如何工作的?
问题描述
我一直在看Core Python编程书,作者举了一个例子:
I have been reading the Core Python programming book, and the author shows an example like:
(4, 5) < (3, 5) # Equals false
所以,我想知道,它如何/为什么等于 false?python如何比较这两个元组?
So, I'm wondering, how/why does it equal false? How does python compare these two tuples?
顺便说一句,书中没有解释.
Btw, it's not explained in the book.
解决方案
元组逐个位置比较:第一个元组的第一项与第二个元组的第一项进行比较;如果它们不相等(即第一个大于或小于第二个),那么这是比较的结果,否则考虑第二个项目,然后考虑第三个等等.
Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal (i.e. the first is greater or smaller than the second) then that's the result of the comparison, else the second item is considered, then the third and so on.
参见通用序列操作:
相同类型的序列也支持比较.特别是,元组和列表通过比较相应的元素来按字典顺序进行比较.这意味着要比较相等,每个元素必须比较相等,并且两个序列必须是相同类型且具有相同长度.
Sequences of the same type also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length.
还有价值比较了解更多详情:
内置集合之间的字典比较工作如下:
Lexicographical comparison between built-in collections works as follows:
- 要比较相等的两个集合,它们必须是相同类型,具有相同长度,并且每对对应的元素必须比较相等(例如,
[1,2] == (1,2)
为假,因为类型不一样). - 支持顺序比较的集合的顺序与其第一个不相等的元素相同(例如,
[1,2,x] <= [1,2,y]
的值与x <= y
).如果对应的元素不存在,则先排序较短的集合(例如,[1,2] < [1,2,3]
为真).
- For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example,
[1,2] == (1,2)
is false because the type is not the same). - Collections that support order comparison are ordered the same as their first unequal elements (for example,
[1,2,x] <= [1,2,y]
has the same value asx <= y
). If a corresponding element does not exist, the shorter collection is ordered first (for example,[1,2] < [1,2,3]
is true).
如果不相等,则序列的排序与其第一个不同的元素相同.例如, cmp([1,2,x], [1,2,y]) 返回与 cmp(x,y) 相同的结果.如果对应的元素不存在,则认为较短的序列较小(例如,[1,2] <[1,2,3] 返回 True).
If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is considered smaller (for example, [1,2] < [1,2,3] returns True).
注1:<
和>
不代表小于".和大于"但在"之前和在之后":所以 (0, 1) 在之前";(1, 0).
Note 1: <
and >
do not mean "smaller than" and "greater than" but "is before" and "is after": so (0, 1) "is before" (1, 0).
注意 2:元组不能被视为 n 维空间中的向量,根据它们的长度进行比较.
Note 2: tuples must not be considered as vectors in a n-dimensional space, compared according to their length.
注3:指问题https://stackoverflow.com/questions/36911617/python-2-tuple-comparison:不要认为元组更大";仅当第一个元素的任何元素大于第二个中的相应元素时才比另一个元素.
Note 3: referring to question https://stackoverflow.com/questions/36911617/python-2-tuple-comparison: do not think that a tuple is "greater" than another only if any element of the first is greater than the corresponding one in the second.
相关文章