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

    • <bdo id='zR9Md'></bdo><ul id='zR9Md'></ul>

    <tfoot id='zR9Md'></tfoot>

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

        <legend id='zR9Md'><style id='zR9Md'><dir id='zR9Md'><q id='zR9Md'></q></dir></style></legend>
      1. Python 2 如何比较字符串和整数?为什么列表比较大于数字,而元组大于列表?

        How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?(Python 2 如何比较字符串和整数?为什么列表比较大于数字,而元组大于列表?)

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

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

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

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

                1. 本文介绍了Python 2 如何比较字符串和整数?为什么列表比较大于数字,而元组大于列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!


                  The following snippet is annotated with the output (as seen on ideone.com):

                  print "100" < "2"      # True
                  print "5" > "9"        # False
                  print "100" < 2        # False
                  print 100 < "2"        # True
                  print 5 > "9"          # False
                  print "5" > 9          # True
                  print [] > float('inf') # True
                  print () > []          # True

                  Can someone explain why the output is as such?

                  Implementation details

                  • Is this behavior mandated by the language spec, or is it up to implementors?
                  • Are there differences between any of the major Python implementations?
                  • Are there differences between versions of the Python language?


                  From the python 2 manual:

                  CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

                  When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

                  When you order a numeric and a non-numeric type, the numeric type comes first.

                  >>> 5 < 'foo'
                  >>> 5 < (1, 2)
                  >>> 5 < {}
                  >>> 5 < [1, 2]

                  When you order two incompatible types where neither is numeric, they are ordered by the alphabetical order of their typenames:

                  >>> [1, 2] > 'foo'   # 'list' < 'str' 
                  >>> (1, 2) > 'foo'   # 'tuple' > 'str'
                  >>> class Foo(object): pass
                  >>> class Bar(object): pass
                  >>> Bar() < Foo()

                  One exception is old-style classes that always come before new-style classes.

                  >>> class Foo: pass           # old-style
                  >>> class Bar(object): pass   # new-style
                  >>> Bar() < Foo()

                  Is this behavior mandated by the language spec, or is it up to implementors?

                  There is no language specification. The language reference says:

                  Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

                  So it is an implementation detail.

                  Are there differences between any of the major Python implementations?

                  I can't answer this one because I have only used the official CPython implementation, but there are other implementations of Python such as PyPy.

                  Are there differences between versions of the Python language?

                  In Python 3.x the behaviour has been changed so that attempting to order an integer and a string will raise an error:

                  >>> '10' > 5
                  Traceback (most recent call last):
                    File "<pyshell#0>", line 1, in <module>
                      '10' > 5
                  TypeError: unorderable types: str() > int()

                  这篇关于Python 2 如何比较字符串和整数?为什么列表比较大于数字,而元组大于列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

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

                    <tfoot id='L75et'></tfoot>

                          <bdo id='L75et'></bdo><ul id='L75et'></ul>

                        • <legend id='L75et'><style id='L75et'><dir id='L75et'><q id='L75et'></q></dir></style></legend>
                            <tbody id='L75et'></tbody>
                          <i id='L75et'><tr id='L75et'><dt id='L75et'><q id='L75et'><span id='L75et'><b id='L75et'><form id='L75et'><ins id='L75et'></ins><ul id='L75et'></ul><sub id='L75et'></sub></form><legend id='L75et'></legend><bdo id='L75et'><pre id='L75et'><center id='L75et'></center></pre></bdo></b><th id='L75et'></th></span></q></dt></tr></i><div id='L75et'><tfoot id='L75et'></tfoot><dl id='L75et'><fieldset id='L75et'></fieldset></dl></div>