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 上下文管理器使用方法小结

上下文管理器最常用的是确保正确关闭文件, with open('/path/to/file', 'r') as f: f.read() with 语句的基本语法, with...

Python socket实现的文件下载器功能示例

本文实例讲述了Python socket实现的文件下载器功能。分享给大家供大家参考,具体如下: 文件下载器 先写客户端再写服务端 1.tcp下载器客户端 import socket...

Python获取SQLite查询结果表列名的方法

本文实例讲述了Python获取SQLite查询结果表列名的方法。分享给大家供大家参考,具体如下: 获得查询结果表的列名: db = sqlite.connect('data.db')...

Python使用Phantomjs截屏网页的方法

实例如下所示: #!/usr/bin/python # -*- coding:utf8 -*- from selenium import webdriver import os d...

pygame实现俄罗斯方块游戏(AI篇2)

pygame实现俄罗斯方块游戏(AI篇2)

继续pygame实现俄罗斯方块游戏(AI篇1)的代码更新 一、消除后才做评价 上一篇我们是对方块落下的位置和落下后出来的空洞进行了评价,但是这些评价都是没有计算消除的,以至于机器人现在不...