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实现按行分割文件的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python #--*-- coding:utf-8 --*--...

nginx搭建基于python的web环境的实现步骤

nginx搭建基于python的web环境的实现步骤

前言: 在搭建开始前,我们先来梳理下web服务工作流程,先看下图: 1、用户(PC)向web服务器发起http请求 2、web服务器判断用户请求文件是否为静态文件,是则直接读取静态文件...

Django使用详解:ORM 的反向查找(related_name)

先定义两个模型,一个是A,一个是B,是一对多的类型。 class A(models.Model): name= models.CharField('名称', max_length...

在Django框架中运行Python应用全攻略

我们来假定下面的这些概念、字段和关系:     一个作者有姓,有名及email地址。     出版商有名称,地址,所...

python字符串编码识别模块chardet简单应用

python的字符串编码识别模块(第三方库): 官方地址: http://pypi.python.org/pypi/chardet import chardet import...