python - 两个元组的最大值

标签 python max

Python 文档指出,当使用多个参数调用时,max() 返回参数中最大的

>>> a = (1, 1, 1, 9)
>>> b = (4, 5, 6)
>>> max(a, b)
(4, 5, 6)

在这种情况下,什么定义了元组的?元组 a 有更多的元素(四个对三个),而且它的最大值 (9) 大于可以在 b 中找到的最大数量 (6 ),所以无论从哪个标准来看,我都希望它是退回的。 max() 如何比较元组?

最佳答案

像所有其他序列一样,元组按字典顺序排序:两个元组的顺序由元组不同的第一个位置决定。引自 python reference :

Tuples and lists are compared lexicographically using comparison of corresponding elements.

你的两个元组在第一个位置上不同,因为 4 > 1,我们有

>>> (4, 5, 6) > (1, 1, 1, 9)
True

关于python - 两个元组的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9575917/

相关文章:

python - 检测 x y 图中尖峰的算法

mysql - 如何找到表中每一列的最大值?

mysql - 在 SQL 中返回所有具有 "MAX"值的行?

sqlite - 在 SQLite 中选择所有具有 MAX 值的行?

php - MYSQL - 选择历史最高分

matlab - 每行中最大值元素的索引(最后一个索引)

python - 如何反向执行 date_range?

python - 在 SQLite3 DELETE 语句中引用 python 变量

python - 使用文件转换器时 PyPy 比 CPython 慢

Python Tkinter : AttributeError: Checkbutton instance has no attribute 'get'