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字符串格式化方式解析

1.%格式符 name = '李四' age = 18 a = "姓名:%s,年龄:%s"%(name,age) print(a) #姓名:李四,年龄:18 ​ b...

对python .txt文件读取及数据处理方法总结

对python .txt文件读取及数据处理方法总结

1、处理包含数据的文件 最近利用Python读取txt文件时遇到了一个小问题,就是在计算两个np.narray()类型的数组时,出现了以下错误: TypeError: ufunc '...

windows下Python安装、使用教程和Notepad++的使用教程

windows下Python安装、使用教程和Notepad++的使用教程

一、Python下载 1.进入Python官网:https://www.python.org/ 2.选择windows版本(Download > Windows) 3.点击下载P...

python字符串切割:str.split()与re.split()的对比分析

1、str.split不支持正则及多个切割符号,不感知空格的数量,比如用空格切割,会出现下面情况。 >>> s1="aa bb cc" >>> s...

Python 错误和异常代码详解

程序中的错误一般被称为 Bug,无可否认,这几乎总是程序员的错。。。 程序员的一生,始终伴随着一件事 - 调试(错误检测、异常处理)。反反复复,最可怕的是:不仅自己的要改,别人的也要改。...