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

相关文章

Queue 实现生产者消费者模型(实例讲解)

Python中,队列是线程间最常用的交换数据的形式。 Python Queue模块有三种队列及构造函数: 1、Python Queue模块的FIFO队列先进先出。 class Queue...

python+pyqt5实现图片批量缩放工具

python+pyqt5实现图片批量缩放工具

批量修改图片大小好像用PS也可以,不过我不会,程序猿就用程序来解决。 这段时间学了下Python,很强大,之前一些不知道怎么处理的东西在Python里面都能找到解决方法。 工具界面如下...

python列表list保留顺序去重的实例

常规通过迭代或set方法,都无法保证去重后的顺序问题 如下,我们可以通过列表的索引功能,对set结果进行序列化 old_list=["a",1,"b","a","b",2,5,1]...

简单了解python代码优化小技巧

简单了解python代码优化小技巧

对比以下两种写法,思考一下为何可以这样写。 成绩在 [0,50)、[50,60)、[60,80)、[80,100)、100、其它 score = float(input("请输入你的...

python绘图方法实例入门

本文实例讲述了python绘图方法。分享给大家供大家参考。具体如下: # -*- coding:utf-8 -*- import matplotlib.pyplot as plt d...