简体   繁体   English

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

[英]How does tuple comparison work in Python?

I have been reading the Core Python<\/em> programming book, and the author shows an example like:我一直在阅读Core Python<\/em>编程书,作者举了一个例子:

(4, 5) < (3, 5) # Equals false

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 (ie 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).两个集合比较相等,它们必须是相同的类型,具有相同的长度,并且每对对应的元素必须比较相等(例如, [1,2] == (1,2)为 false,因为类型不一样)。
  • 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 ).支持顺序比较的集合的顺序与其第一个不相等的元素相同(例如, [1,2,x] <= [1,2,y]x <= y具有相同的值)。 If a corresponding element does not exist, the shorter collection is ordered first (for example, [1,2] < [1,2,3] is true).如果对应的元素不存在,则先排序较短的集合(例如, [1,2] < [1,2,3]为真)。

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).例如, cmp([1,2,x], [1,2,y]) 返回与 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,2] < [1,2,3] 返回 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).注 1<>不表示“小于”和“大于”,而是表示“之前”和“之后”:所以 (0, 1) “之前” (1, 0)。

Note 2 : tuples must not be considered as vectors in a n-dimensional space , compared according to their length.注意 2 :元组不能被视为n 维空间中的向量,根据它们的长度进行比较。

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.注3 :参考问题https://stackoverflow.com/questions/36911617/python-2-tuple-comparison :不要认为一个元组比另一个元组“更大”只有当第一个元素大于相应的一个在第二个。

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')<\/code>

A is converted to its corresponding ASCII ord('A') #65<\/code> same for other elements A 转换为其对应的 ASCII ord('A') #65<\/code>与其他元素相同

So, >> a>b # True<\/code> you can think of it as comparing between string (It is exactly, actually)所以, >> a>b # True<\/code>你可以把它看作是字符串之间的比较(实际上是这样)

the same thing goes for integers too.同样的事情也适用于整数。

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

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 -)因为(1 不大于 1,移动到下一个,2 不大于 2,移动到下一个 2 小于三个 - 按字典顺序 -)

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.将其视为一个元素在另一个按字母顺序排列的元素之前,不是元素大于一个元素,在这种情况下,将所有元组元素视为一个字符串。

<\/blockquote><\/blockquote>"

Comparing tuples 比较元组

A comparison operator in Python can work with tuples. Python中的比较运算符可以使用元组。 The comparison starts with a first element of each tuple. 比较从每个元组的第一个元素开始。 If they do not compare to = , < or > then it proceed to the second element and so on. 如果它们不等于=<>则继续进行第二个元素,依此类推。

Let's study this with an example: 让我们用一个例子研究一下:

    #case 1
    a=(5,6)
    b=(1,4)
    if (a>b):print("a is bigger")
    else: print("b is bigger")

    #case 2
    a=(5,6)
    b=(5,4)
    if (a>b):print("a is bigger")
    else: print ("b is bigger")

    #case 3
    a=(5,6)
    b=(6,4)
    if (a>b):print("a is bigger")
    else: print("b is bigger")

Case1: 情况1:

Comparison starts with a first element of each tuple. 比较从每个元组的第一个元素开始。 In this case 5>1, so the output a is bigger 在这种情况下5> 1,因此输出a更大

Case 2: 情况2:

Comparison starts with a first element of each tuple. 比较从每个元组的第一个元素开始。 In this case 5>5 which is inconclusive. 在这种情况下5> 5是不确定的。 So it proceeds to the next element. 因此,它进行到下一个元素。 6>4, so the output a is bigger 6> 4,所以输出a更大

Case 3: 情况3:

Comparison starts with a first element of each tuple. 比较从每个元组的第一个元素开始。 In this case 5>6 which is false. 在这种情况下5> 6是错误的。 So it goes into the else loop prints "b is bigger." 因此它进入else循环打印“ b更大”。

conclusion: 结论:

Tuples always compare its first index and gives output according to your program. 元组总是比较它的第一个索引,并根据您的程序给出输出。 it does not compare all the elements. 它不会比较所有元素。

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

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