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

相关文章

解析PyCharm Python运行权限问题

解析PyCharm Python运行权限问题

先通过 which python 获得 python 指令所在路径: $ which python /usr/bin/python 如上得到了其所在路径是 /usr/bin/pyt...

Tensorflow实现神经网络拟合线性回归

Tensorflow实现神经网络拟合线性回归

本文实例为大家分享了Tensorflow实现神经网络拟合线性回归的具体代码,供大家参考,具体内容如下 一、利用简单的一层神经网络拟合一个函数 y = x^2 ,其中加入部分噪声作为偏置值...

Python中print函数简单使用总结

Python中print函数简单使用总结

print函数是Python的入门,每一个学习python的人都绕不开这个函数,下面介绍一下这个函数的用法。 打开电脑,选择python软件,下面选择python 3.7为例进行介绍,点...

一文带你了解Python中的字符串是什么

一文带你了解Python中的字符串是什么

在《 详解Python拼接字符串的七种方式 》这篇文章里,我提到过,字符串是程序员离不开的事情。后来,我看到了一个英文版本的说法: There are few guarantees in...

Python实现的计数排序算法示例

Python实现的计数排序算法示例

本文实例讲述了Python实现的计数排序算法。分享给大家供大家参考,具体如下: 计数排序是一种非常快捷的稳定性强的排序方法,时间复杂度O(n+k),其中n为要排序的数的个数,k为要排序的...