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

yipeiwu_com5年前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 利用内置set函数对字符串和列表进行去重的方法

如下所示: # coding:utf8 __author__ = 'libingxian' __date = "20170415" # 由于数据类型set本身具有无序,唯一值的特性...

Python 限制线程的最大数量的方法(Semaphore)

如下所示: import threading import time sem=threading.Semaphore(4) #限制线程的最大数量为4个 def gothrea...

Python下的Softmax回归函数的实现方法(推荐)

Python下的Softmax回归函数的实现方法(推荐)

Softmax回归函数是用于将分类结果归一化。但它不同于一般的按照比例归一化的方法,它通过对数变换来进行归一化,这样实现了较大的值在归一化过程中收益更多的情况。 Softmax公式 S...

Pytorch的mean和std调查实例

如下所示: # coding: utf-8 from __future__ import print_function import copy import click impor...

python根据日期返回星期几的方法

本文实例讲述了python根据日期返回星期几的方法。分享给大家供大家参考。具体如下: 这个函数给定日期,输出星期几,至于0是星期一还是星期天,这和时区有关,反正我这里是0表示星期一...