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

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

问题描述

以下代码段带有输出注释(在 ideone.com 上看到):

打印100"<"2" # 真打印5">"9" # 错误打印100"<2 # 错误打印 100 <"2" # 真打印 5 >"9" # 错误打印5">9 # 真打印 [] >float('inf') # 真打印 () >[]          # 真的

有人可以解释为什么输出是这样的吗?

<小时>

实施细节

  • 这种行为是语言规范强制要求的,还是由实现者决定?
  • 主要 Python 实现之间是否存在差异?
  • Python 语言版本之间是否存在差异?

解决方案

来自 python 2 手册:

<块引用>

CPython 实现细节:除数字外不同类型的对象按类型名称排序;不支持正确比较的相同类型的对象按其地址排序.

当您对两个字符串或两个数字类型进行排序时,排序以预期的方式完成(字符串的字典顺序,整数的数字排序).

当您订购数字和非数字类型时,数字类型在前.

<预><代码>>>>5<'富'真的>>>5<(1, 2)真的>>>5<{}真的>>>5<[1, 2]真的

当您对两个都不是数字的不兼容类型进行排序时,它们将按其类型名称的字母顺序排序:

<预><代码>>>>[1, 2] >'foo' # '列表' <'str'错误的>>>(1, 2) >'foo' # '元组' >'str'真的>>>类 Foo(对象):通过>>>类 Bar(object): 通过>>>酒吧()<富()真的

一个例外是老式类总是出现在新式类之前.

<预><代码>>>>class Foo: pass # old-style>>>class Bar(object): pass # new-style>>>酒吧()<富()错误的

<小时><块引用>

这种行为是语言规范强制要求的,还是由实现者决定?

没有语言规范.语言参考 说:

<块引用>

否则,不同类型的对象总是比较不相等,并且顺序一致但任意.

所以这是一个实现细节.

<块引用>

主要 Python 实现之间是否存在差异?

我无法回答这个问题,因为我只使用了官方的 CPython 实现,但还有其他 Python 实现,例如 PyPy.

<块引用>

Python 语言版本之间是否存在差异?

在 Python 3.x 中,行为已更改,因此尝试对整数和字符串进行排序将引发错误:

<预><代码>>>>'10' >5回溯(最近一次调用最后一次):文件<pyshell#0>",第 1 行,在 <module> 中'10' >5类型错误:无法排序的类型:str() >整数()

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'
True
>>> 5 < (1, 2)
True
>>> 5 < {}
True
>>> 5 < [1, 2]
True

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' 
False
>>> (1, 2) > 'foo'   # 'tuple' > 'str'
True

>>> class Foo(object): pass
>>> class Bar(object): pass
>>> Bar() < Foo()
True

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()
False


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 如何比较字符串和整数?为什么列表比数字大,元组比列表大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆