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

发布于 2025-01-12 06:44:50 字数 175 浏览 0 评论 0原文

我一直在阅读《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一笑百媚生 2025-01-19 06:44:50

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

请参阅常见序列操作

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

另请参阅值比较了解更多详细信息:

内置集合之间的词典比较工作如下:

  • 要使两个集合比较相等,它们必须具有相同的类型、相同的长度,并且每对对应的元素必须比较相等(例如,[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:参考问题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:

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.

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 ends

千と千尋 2025-01-19 06:44:50

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.

我三岁 2025-01-19 06:44:50

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.

皇甫轩 2025-01-19 06:44:50

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文