python基本数据类型大小比较

查看:187
本文介绍了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

很明显了吧

这篇关于python基本数据类型大小比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆