• <tfoot id='vW6Aw'></tfoot>

      <bdo id='vW6Aw'></bdo><ul id='vW6Aw'></ul>
  • <small id='vW6Aw'></small><noframes id='vW6Aw'>

        <legend id='vW6Aw'><style id='vW6Aw'><dir id='vW6Aw'><q id='vW6Aw'></q></dir></style></legend>
        <i id='vW6Aw'><tr id='vW6Aw'><dt id='vW6Aw'><q id='vW6Aw'><span id='vW6Aw'><b id='vW6Aw'><form id='vW6Aw'><ins id='vW6Aw'></ins><ul id='vW6Aw'></ul><sub id='vW6Aw'></sub></form><legend id='vW6Aw'></legend><bdo id='vW6Aw'><pre id='vW6Aw'><center id='vW6Aw'></center></pre></bdo></b><th id='vW6Aw'></th></span></q></dt></tr></i><div id='vW6Aw'><tfoot id='vW6Aw'></tfoot><dl id='vW6Aw'><fieldset id='vW6Aw'></fieldset></dl></div>
      1. 元组比较在 Python 中是如何工作的?

        How does tuple comparison work in Python?(元组比较在 Python 中是如何工作的?)

        <small id='LfiZ7'></small><noframes id='LfiZ7'>

          <legend id='LfiZ7'><style id='LfiZ7'><dir id='LfiZ7'><q id='LfiZ7'></q></dir></style></legend>
            <tfoot id='LfiZ7'></tfoot>
              <bdo id='LfiZ7'></bdo><ul id='LfiZ7'></ul>

                  <tbody id='LfiZ7'></tbody>
              1. <i id='LfiZ7'><tr id='LfiZ7'><dt id='LfiZ7'><q id='LfiZ7'><span id='LfiZ7'><b id='LfiZ7'><form id='LfiZ7'><ins id='LfiZ7'></ins><ul id='LfiZ7'></ul><sub id='LfiZ7'></sub></form><legend id='LfiZ7'></legend><bdo id='LfiZ7'><pre id='LfiZ7'><center id='LfiZ7'></center></pre></bdo></b><th id='LfiZ7'></th></span></q></dt></tr></i><div id='LfiZ7'><tfoot id='LfiZ7'></tfoot><dl id='LfiZ7'><fieldset id='LfiZ7'></fieldset></dl></div>

                1. 本文介绍了元组比较在 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
                  

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

                  还有价值比较了解更多详情:

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

                  Lexicographical comparison between built-in collections works as follows:

                  • 要比较相等的两个集合,它们必须是相同类型,具有相同长度,并且每对对应的元素必须比较相等(例如,[1,2] == (1,2) 为假,因为类型不一样).
                  • 支持顺序比较的集合的顺序与其第一个不相等的元素相同(例如,[1,2,x] <= [1,2,y] 的值与x <= y).如果对应的元素不存在,则先排序较短的集合(例如,[1,2] < [1,2,3] 为真).
                  • 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 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <i id='tCW56'><tr id='tCW56'><dt id='tCW56'><q id='tCW56'><span id='tCW56'><b id='tCW56'><form id='tCW56'><ins id='tCW56'></ins><ul id='tCW56'></ul><sub id='tCW56'></sub></form><legend id='tCW56'></legend><bdo id='tCW56'><pre id='tCW56'><center id='tCW56'></center></pre></bdo></b><th id='tCW56'></th></span></q></dt></tr></i><div id='tCW56'><tfoot id='tCW56'></tfoot><dl id='tCW56'><fieldset id='tCW56'></fieldset></dl></div>

                      <legend id='tCW56'><style id='tCW56'><dir id='tCW56'><q id='tCW56'></q></dir></style></legend>

                      <small id='tCW56'></small><noframes id='tCW56'>

                        <bdo id='tCW56'></bdo><ul id='tCW56'></ul>
                          <tbody id='tCW56'></tbody>
                        <tfoot id='tCW56'></tfoot>