python基本数据类型大小比较
PHP中文网
PHP中文网 2017-04-17 17:59:42
0
1
599

偶然间发现这个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中文网

认证高级PHP讲师

reply all(1)
Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template