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

偶然间发现这个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讲师

Antworte allen(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.

用名字排序的 不同类型

列表 list
整数 int

很明显了吧

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!