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

相关文章

python3结合openpyxl库实现excel操作的实例代码

一.相关说明: 1、openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件;2007一下的版本为xls结尾的文件,需要使用 xlrd和xlwt库进行...

matplotlib作图添加表格实例代码

matplotlib作图添加表格实例代码

本文所示代码主要是通过Python+matplotlib实现作图,并且在图中添加表格的功能,具体如下。 代码 import matplotlib.pyplot as plt impo...

Python3获取电脑IP、主机名、Mac地址的方法示例

本文实例讲述了Python3获取电脑IP、主机名、Mac地址的方法。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #! python3 ''' Cr...

pytorch: Parameter 的数据结构实例

一般来说,pytorch 的Parameter是一个tensor,但是跟通常意义上的tensor有些不一样 1) 通常意义上的tensor 仅仅是数据 2) 而Parameter所对应的...

Ubuntu16.04/树莓派Python3+opencv配置教程(分享)

无论是Windows、Linux、还是树莓派 。配置python3的opencv环境都是让人头大的一件事情,尤其是许多人用pip安装以后,发现opencv虽然装上了,但是却装在了系统原生...