Python 不同对象比较大小示例探讨

yipeiwu_com6年前Python基础

万恶的源泉:

Fireboo的疑问(当然 lambda 本身写的就有问题):

>>> filter( lambda x: x > 2, [ 1, [ 1, 2, 3 ], 2, 3 ] ) 
[[1, 2, 3], 3]

?:

>>> 1 < [ 1 ] 
True 
>>> int < list 
True 
>>> dict < int < list 
True
>>> int < map 
False

后来几经周折,和 Fireboo 讨论了下,是

1.不同对象比较(除了 number 之外),是按照 type names 比较,

2.当相同类型对象不支持适当比较的时候,采用 address 比较

3.list 与 list, tuple 与 tuple 采用字典序比较

>>> x = 1 
>>> y = [ 1 ] 
>>> type( x ) 
<type 'int'> 
>>> type( y ) 
<type 'list'> 
>>> x < y 
True
>>> type( int ) 
<type 'type'> 
>>> type( list ) 
<type 'type'> 
>>> id( int ) 
505552912 
>>> id( list ) 
505555336 
>>> int < list 
True
>>> type( map ) 
<type 'builtin_function_or_method'> 
>>> type( list ) 
<type 'type'> 
>>> map < list 
True

相关文章

Python缩进和冒号详解

Python缩进和冒号详解

对于Python而言代码缩进是一种语法,Python没有像其他语言一样采用{}或者begin...end分隔代码块,而是采用代码缩进和冒号来区分代码之间的层次。 缩进的空白数量是可变的,...

在Python中字典根据多项规则排序的方法

我们做登录的时候经常会使用到,验证手机号是否正确、向手机发送验证码倒计时60s的问题,我们改如何解决呢?让我们一起来探讨一下吧。如下图: 首先,我们先说说判断手机号码是否正确的问题吧...

python用装饰器自动注册Tornado路由详解

第一个版本 在这个版本中,首先创建了 RouterConfig 对象,其构造方法创建了 tornado.web.Application() 并赋值为 self.Application ,...

python统计字母、空格、数字等字符个数的实例

如下所示: # -*- coding: utf-8 -*- # 要求:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 def count(s): count_a...

python 在指定范围内随机生成不重复的n个数实例

python 在指定范围内随机生成不重复的n个数实例

利用Python中的randomw.sample()函数实现 resultList=random.sample(range(A,B),N); #表示从[A,B]间随机生成N个数,结...