I have been reading the Core Python programming book and the author shows an example like:

(4,5) < (3,5) # Equals false

So I'm wondering, how/why does it equal false? How does python compare these two tuples?

Btw, it's not explained in the book.

解决方案

Tuples are compared position by position:

the first item of first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on.

See doc:

Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length.

Also this:

Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is considered smaller (for example, [1,2] < [1,2,3] returns True).

Note that < and > do not mean "smaller then" and "greater then" but "is before" and "is after": so (0, 1) "is before" (1, 0).

Note 2: tuples must not be considered as coordinates in a n-dimensional space!

Note 3: referring to question Python 2 tuple comparison: do not think that a tuple is "greater" than another only if any element of the first is greater than the corresponding one in the second

python中元组可以比较大小吗_python元组比较相关推荐

  1. python中字母数字的大小比较_python列表怎么比较大小

    Python中可以使用cmp()函数比较两个列表的大小. cmp()函数语法:cmp(list1, list2) 参数: list1 -- 比较的列表.list2 -- 比较的列表. 返回值: 如果比 ...

  2. [转载] python中set函数是什么数据类型_Python基本数据类型-list-tuple-dict-set详解

    参考链接: Python中的isdisjoint函数 Python基本数据类型-list-tuple-dict-set 数据类型 表示方法 特性 list 列表用方括号表示:[] list是一种有序的 ...

  3. python中的列表分片详解_python列表与元组详解实例

    在这章中引入了数据结构的概念.数据结构是通过某种方式组织在一起的数据元素的集合.在python中,最基本的数据结构就是序列.序列中的每个元素被分配一个序号,即元素的位置,也被称为索引.注意:第一个索引 ...

  4. python中的可变数据类型有列表和元组_Python中列表的 += 和 .extend() 的异同

    一道Python题 最近有朋友"考"了我一个Python的题:使用+=和.extend()两种方法扩展元组中的列表会发生什么.虽然我对Python中的可变数据类型.不可变数据类型的 ...

  5. python中求包含5的数_Python 内置函数 ( ) 可以返回列表、元组、字典、集合、字符串以及 range 对象中元素个数。_学小易找答案...

    [简答题]实例1:求两数相除的结果. 先后输入2个数据,计算第一个数除以第二个数的结果. 要求能够处理输入数据为非数字.除数为零.文件末尾EndOfFile 和用户使用Ctrl + C 命令终止程序等 ...

  6. python中列表是什么样的数据结构_Python中列表、字典、元组、集合数据结构整理...

    Python常见数据结构整理 Python中常见的数据结构可以统称为容器(container).序列(如列表和元组).映射(如字典)以及集合(set)是三类主要的容器. 一.序列(列表.元组和字符串) ...

  7. python中如何比较日期大小_python 日期大小比较

    输入的日期跟当前日期比较大小 日期在python中 存在time,datetime,string三种形式转化如下 #str转time time.strptime('2018-09-28'," ...

  8. python中int表示的数据类型是_python中的基本数据类型之 int bool str

    一.基本数据类型 1. int  ==>  整数.主要用来进行数学运算. 2.str  ==>  字符串.可以保存少量的数据,并进行相应的操作. 3.bool  =>  布尔值.判断 ...

  9. python中数字应该用什么表示_Python

    Python Number(数字) Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. 以下实例在变量赋值 ...

最新文章

  1. 为什么C语言不会过时?
  2. 图像去畸变矫正及双线性内插法
  3. Unity_UIWidgets学习笔记03_组件_Container
  4. shell脚本自动回车_干货分享:Linux shell脚本分享!!快收藏起来吧
  5. linux服务器 32核,Intel换成AMD 32核锐龙 Linux之父体验很好
  6. QT的QDomElement类的使用
  7. Java黑皮书课后题第6章:6.12(显示字符)使用下面的方法头,编写一个打印字符的方法。编写一个测试程序、打印从‘1‘到‘Z‘的字符,每行打印10个,字符之间使用一个空格字符隔开
  8. Matcher类的简单使用
  9. stream对多个字段分组_java8 stream 如何按多字段分组,并对一个字段求和
  10. jquery[学习心得]ajax的注意点
  11. WebService 代码创建E9流程
  12. 【CodeForces 1260D --- A Game with Traps】二分
  13. RTSP安防网络摄像头/海康大华硬盘录像机网页无插件直播流媒体服务器EasyNVR授权方式之加密机如何成功授权
  14. 泛化误差,偏差方差分解
  15. 5G核心网-Identifiers身份标识
  16. could not initialize javavm mysql_Could not initialize JavaVM
  17. Typora自制主题
  18. 大数据职业理解_大数据岗位介绍和职业规划分析
  19. ImageJ 叠加明场和荧光场图片
  20. Synopsys Formality 2018操作流程

热门文章

  1. 杜比收购低延迟流媒体平台Millicast
  2. 全域调度:云边协同在视频场景下的探索实践
  3. LiveVideoStack 2021招聘季
  4. 2021 有哪些不容错过的后端技术趋势
  5. Storm 1.1.0 集群安装
  6. Core ML介绍 (Apple机器学习框架)
  7. redis事务原理,使用,详解
  8. go benchmark 性能测试 单元测试 基准测试 使用方法详解
  9. javaweb---三层架构++
  10. 307. Range Sum Query - Mutable | 307. 区域和检索 - 数组可修改(数据结构:线段树,图文详解)