Python如何比较字符串和int?

时间:2021-11-05 22:47:45

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

下面的片段用输出进行了注释(如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

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?
  • 主要的Python实现之间有什么区别吗?
  • Are there differences between versions of the Python language?
  • Python语言的版本之间有区别吗?

2 个解决方案

#1


194  

From the python 2 manual:

python 2手册:

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.

CPython实现细节:除了数字之外,不同类型的对象是按它们的类型名称排序的;不支持适当比较的相同类型的对象按其地址排序。

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?

主要的Python实现之间有什么区别吗?

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.

我不能回答这个问题,因为我只使用了官方的CPython实现,但是还有其他的Python实现,比如PyPy。

Are there differences between versions of the Python language?

Python语言的版本之间有区别吗?

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

Python 3。x的行为发生了改变,因此试图订购一个整数和一个字符串会引起错误:

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

#2


20  

Strings are compared lexicographically, and dissimilar types are compared by the name of their type ("int" < "string"). 3.x fixes the second point by making them non-comparable.

字符串在字典上进行比较,不同类型通过类型的名称(“int”<“string”)进行比较。3所示。x通过使它们不可比较来修正第二点。

#1


194  

From the python 2 manual:

python 2手册:

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.

CPython实现细节:除了数字之外,不同类型的对象是按它们的类型名称排序的;不支持适当比较的相同类型的对象按其地址排序。

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?

主要的Python实现之间有什么区别吗?

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.

我不能回答这个问题,因为我只使用了官方的CPython实现,但是还有其他的Python实现,比如PyPy。

Are there differences between versions of the Python language?

Python语言的版本之间有区别吗?

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

Python 3。x的行为发生了改变,因此试图订购一个整数和一个字符串会引起错误:

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

#2


20  

Strings are compared lexicographically, and dissimilar types are compared by the name of their type ("int" < "string"). 3.x fixes the second point by making them non-comparable.

字符串在字典上进行比较,不同类型通过类型的名称(“int”<“string”)进行比较。3所示。x通过使它们不可比较来修正第二点。