元组比较在Python中如何工作?

问题:元组比较在Python中如何工作?

我一直在阅读Core Python编程书,作者展示了一个类似的示例:

(4, 5) < (3, 5) # Equals 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.


回答 0

比较元组的位置:将第一元组的第一项与第二元组的第一项进行比较;如果它们不相等(即第一个大于或小于第二个),则这是比较的结果,否则将考虑第二个,然后是第三个,依此类推。

请参阅常见序列操作

相同类型的序列也支持比较。特别是,通过比较相应的元素按字典顺序比较了元组和列表。这意味着要比较相等,每个元素必须比较相等,并且两个序列必须具有相同的类型并且具有相同的长度。

还可以进行值比较以获取更多详细信息:

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

  • 为了使两个集合比较相等,它们必须是同一类型,具有相同的长度,并且每对对应的元素必须比较相等(例如, [1,2] == (1,2)由于类型不同为false)。
  • 支持顺序比较的集合的排序与其第一个不相等元素相同(例如,[1,2,x] <= [1,2,y]具有与相同的值x <= y)。如果不存在相应的元素,则将对较短的集合进行排序(例如,[1,2] < [1,2,3]为true)。

如果不相等,则序列与它们的第一个不同元素的排序相同。例如,cmp([1,2,x],[1,2,y])返回的结果与cmp(x,y)相同。如果不存在相应的元素,则较短的序列被视为较小的序列(例如[1,2] <[1,2,3]返回True)。

注1<>并不意味着与“大于”,“小于”,而是“是之前”和“之后”:所以(0,1)“是之前”(1,0)。

注2:根据元组的长度,元组不能视为n维空间中的向量

注意3:参考问题/programming/36911617/python-2-tuple-comparison:仅当第一个元组的任何元素大于对应的元组时,才认为该元组比另一个元组“更大”一秒。

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:

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:

  • 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).

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.


回答 1

Python文档做解释。

使用对应元素的比较,按字典顺序比较元组和列表。这意味着要比较相等,每个元素必须比较相等,并且两个序列必须具有相同的类型并且长度相同。

The Python documentation does explain it.

Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.


回答 2

Python 2.5的文档解释了它做好。

使用对应元素的比较,按字典顺序比较元组和列表。这意味着要比较相等,每个元素必须比较相等,并且两个序列必须具有相同的类型并且具有相同的长度。

如果不相等,则序列与它们的第一个不同元素的排序相同。例如,cmp([1,2,x],[1,2,y])返回的结果与cmp(x,y)相同。如果相应的元素不存在,则较短的序列首先被排序(例如[1,2] <[1,2,3])。

不幸的是,该页面似乎在文档的最新版本中消失了。

The python 2.5 documentation explains it well.

Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

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 ordered first (for example, [1,2] < [1,2,3]).

Unfortunately that page seems to have disappeared in the documentation for more recent versions.


回答 3

在进行整数比较之前,我有些困惑,因此我将通过一个示例来说明它对初学者更友好

a = ('A','B','C') # see it as the string "ABC" b = ('A','B','D')

A转换为其对应的ASCII ord('A') #65与其他元素相同的

因此, >> a>b # True 您可以将其视为字符串之间的比较(确实如此)

整数也是如此。

x = (1,2,2) # see it the string "123" y = (1,2,3) x > y # False

因为(1不大于1,移至下一个,2不大于2,移至下一个2小于三-按字典顺序-)

上面的答案中提到了关键点

认为它是一个元素,在另一个字母前不是一个元素大于一个元素,在这种情况下,将所有元组元素视为一个字符串。

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 elements

So, >> 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

think of it as an element is before another alphabetically not element is greater than an element and in this case consider all the tuple elements as one string.