首页 > python基本数据类型大小比较

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

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


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.

用名字排序的 不同类型

列表 list
整数 int

很明显了吧

【热门文章】
【热门文章】