日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

python中str和int区别_Python如何比较string和int?

發(fā)布時(shí)間:2025/4/16 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中str和int区别_Python如何比较string和int? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

當(dāng)您訂購(gòu)兩個(gè)字符串或兩個(gè)數(shù)字類型時(shí),排序是以預(yù)期的方式完成的(字符串的字典排序,整數(shù)的數(shù)字排序)。

當(dāng)您訂購(gòu)數(shù)字和非數(shù)字類型時(shí),數(shù)字類型優(yōu)先。

>>> 5 < 'foo'

True

>>> 5 < (1, 2)

True

>>> 5 < {}

True

>>> 5 < [1, 2]

True

當(dāng)您訂購(gòu)兩個(gè)不兼容的類型(兩者都不是數(shù)字)時(shí),它們按其類型名的字母順序排序:

>>> [1, 2] > 'foo' # 'list' < 'str'

False

>>> (1, 2) > 'foo' # 'tuple' > 'str'

True

>>> class Foo(object): pass

>>> class Bar(object): pass

>>> Bar() < Foo()

True

一個(gè)例外是舊式類,它總是在新式類之前。

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

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

所以這是一個(gè)實(shí)現(xiàn)細(xì)節(jié)。

Are there differences between any of the major Python implementations?

我不能回答這一個(gè),因?yàn)槲抑皇褂霉俜降腃Python實(shí)現(xiàn),但還有其他的Python實(shí)現(xiàn),如PyPy。

Are there differences between versions of the Python language?

在Python 3.x中,行為已經(jīng)改變,因此嘗試排序整數(shù)和字符串將引發(fā)錯(cuò)誤:

>>> '10' > 5

Traceback (most recent call last):

File "", line 1, in

'10' > 5

TypeError: unorderable types: str() > int()

總結(jié)

以上是生活随笔為你收集整理的python中str和int区别_Python如何比较string和int?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。