元组比较在Python中如何工作? [英] How does tuple comparison work in Python?

查看:43
本文介绍了元组比较在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

所以,我想知道它如何/为什么等于假? 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.

值比较以获取更多详细信息:

Also Value Comparisons for further details:

内置集合之间的词法比较如下:

Lexicographical comparison between built-in collections works as follows:


  • 要使两个集合相等,它们必须是相同的类型,具有相同的长度,并且每对对应的元素必须比较相等(例如, [1,2] ==(1,2)为false,因为类型不同)。

  • 支持顺序比较的集合的排序与其第一个不相等元素相同(例如, [1,2,x]< ; = [1,2,y] x< = y 具有相同的值)。如果不存在相应的元素,则将对较短的集合进行排序(例如, [1,2]< [1,2,3] 为真)。 / li>
  • 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 as x <= 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.

这篇关于元组比较在Python中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆