search

Home  >  Q&A  >  body text

python基本数据类型大小比较

偶然间发现这个1个有意思的问题,比如我让列表与数字进行比较,可以发现恒为真。

>>> []>10**100
True

另外,如果用python的其他基本数据类型进行比较可以得到这样的结果。

>>> []>{}
True
>>> []>set()
False
>>> set()>'1'
False
>>> 1>1.0
False
>>> 1>=1.0
True
>>> '1'>1.0
True
>>> set()>1.0
True
>>> [1]>[2]
False
>>> [1]>[0]
True

按照这种比较可以发现整数是最小的。为什么会出现列表比数字大这样的情况呢?

PHP中文网PHP中文网2813 days ago606

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 18:01:42

    https://docs.python.org/2/library/stdtypes.html#comparisons

    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.

    Different types sorted by name

    list list
    integer int

    It’s obvious

    reply
    0
  • Cancelreply