Python 中的元组比较是如何工作的?
我一直在阅读《Core Python》编程书,作者展示了一个示例,例如:
(4, 5) < (3, 5) # Equals false
所以,我想知道,它如何/为什么等于 false? python如何比较这两个元组?
顺便说一句,书上没有解释。
I have been reading the Core Python programming book, and the author shows an example like:
(4, 5) < (3, 5) # Equals false
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
元组按位置进行比较:
将第一个元组的第一项与第二个元组的第一项进行比较;如果它们不相等(即第一个大于或小于第二个),则这就是比较的结果,否则考虑第二项,然后考虑第三项,依此类推。
请参阅常见序列操作:
另请参阅值比较了解更多详细信息:
如果不相等,则序列的排序与其第一个不同元素的排序相同。例如,cmp([1,2,x], [1,2,y]) 返回与 cmp(x,y) 相同的结果。如果对应的元素不存在,则较短的序列被认为较小(例如,[1,2] < [1,2,3] 返回 True)。
注1:
<
和>
并不表示“小于”和“大于”,而是表示“在之前”和“在之后” ”:所以 (0, 1) “在”(1, 0) 之前。注2:元组不能被视为n维空间中的向量,根据其长度进行比较。
注3:参考问题https://stackoverflow.com/questions/36911617 /python-2-tuple-comparison:仅当第一个元组的任何元素大于第二个元组中的相应元素时,才认为一个元组比另一个元组“更大”。
注4:正如@david Winiecki在评论中提到的,如果有两个长度不同的元组,则第一个到达末尾的元组(与前一部分相等)被声明为较低的元组:
(1, 2) < (1, 2, 3)
,因为 1=1, 2=2 然后第一个元组结束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.
See Common Sequence Operations:
Also Value Comparisons for further details:
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).
Note 1:
<
and>
do not mean "smaller than" and "greater than" but "is before" and "is after": so (0, 1) "is before" (1, 0).Note 2: tuples must not be considered as vectors in a n-dimensional space, compared according to their length.
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.
Note 4: as @david Winiecki mentioned in the comments, in case of two tuples of different length, the first one which reaches its end, being the previous part equal, is declared as the lower:
(1, 2) < (1, 2, 3)
, since 1=1, 2=2 and then the first tuple endsPython 文档确实对此进行了解释。
The Python documentation does explain it.
python 2.5 文档很好地解释了这一点。
不幸的是,该页面似乎已在最新版本的文档中消失。
The python 2.5 documentation explains it well.
Unfortunately that page seems to have disappeared in the documentation for more recent versions.
I had some confusion before regarding integer comparsion, so I will explain it to be more beginner friendly with an example
a = ('A','B','C') # 将其视为字符串“ABC”
b = ('A','B','D')
A 被转换为其对应的 ASCII
ord('A') #65
对于其他元素也是如此,
<代码>>> a>b # 正确
您可以将其视为字符串之间的比较(实际上确实如此),
同样的事情也适用于整数。
x = (1,2,2) # 看到字符串“123”
y = (1,2,3)
x> y # False
因为(1不大于1,移至下一个,2不大于2,移至下一个2小于3-按字典顺序-)
关键点在上面的答案中提到了
I had some confusion before regarding integer comparsion, so I will explain it to be more beginner friendly with an example
a = ('A','B','C') # see it as the string "ABC"
b = ('A','B','D')
A is converted to its corresponding ASCII
ord('A') #65
same for other elementsSo,
>> a>b # True
you can think of it as comparing between string (It is exactly, actually)
the same thing goes for integers too.
x = (1,2,2) # see it the string "123"
y = (1,2,3)
x > y # False
because (1 is not greater than 1, move to the next, 2 is not greater than 2, move to the next 2 is less than three -lexicographically -)
The key point is mentioned in the answer above