Python 内置 max(),当 2 个值相同时选择哪个?
Python built-in max(), when 2 values are the same which one gets picked?
我有一个代码可以根据某些日期处理数据。
让我们说:
case1:
values1 with date1 = '2002-02-01'
values2 with date2 = '2004-02-01'
case2:
values1 with date1 ='2001-01-01'
values2 with date2 ='2001-01-01'
我需要获取最新的记录。当我的值具有不同的日期但记录具有相同的日期 max(date1, date2)
时一切正常。
问题。当值相等时返回哪个最大值,如情况 2?
"If multiple items are maximal, the function returns the first one encountered."
如果多个值都是最大值,则返回第一个这样的值:
>>> class Equal:
... def __init__(self, id):
... self.id = id
... def __repr__(self):
... return f"Equal({self.id!r})"
... def __gt__(self, other):
... return False
...
>>> max([Equal(1), Equal(2), Equal(3)])
Equal(1)
这是明确的 documented:
If multiple items are maximal, the function returns the first one encountered.
我有一个代码可以根据某些日期处理数据。 让我们说:
case1:
values1 with date1 = '2002-02-01'
values2 with date2 = '2004-02-01'
case2:
values1 with date1 ='2001-01-01'
values2 with date2 ='2001-01-01'
我需要获取最新的记录。当我的值具有不同的日期但记录具有相同的日期 max(date1, date2)
时一切正常。
问题。当值相等时返回哪个最大值,如情况 2?
"If multiple items are maximal, the function returns the first one encountered."
如果多个值都是最大值,则返回第一个这样的值:
>>> class Equal:
... def __init__(self, id):
... self.id = id
... def __repr__(self):
... return f"Equal({self.id!r})"
... def __gt__(self, other):
... return False
...
>>> max([Equal(1), Equal(2), Equal(3)])
Equal(1)
这是明确的 documented:
If multiple items are maximal, the function returns the first one encountered.