如果是列表或元組,Returns the smaller subscript of the concatenated value(i.e. the earlier value):
print(max([5.0, 5, 3, 1]))
print(max([3, 1, 5, 5.0]))
輸出:
5.0
5
如果是集合,It returns the one that was traversed earlier in the traversal:
nums = [2, 1, 1, 3, 2]
print(max(set(nums), key=nums.count))
輸出:
1
雖然集合是無序的,但是對於{1, 2, 3}這樣的集合,Every time the order is1, 2, 3,So it can be determined1.
For most sets it is random:
chars = ['b', 'c', 'a', 'a', 'c']
print(chars_set := set(chars))
print(max(chars_set, key=chars.count))
輸出:
{
'b', 'a', 'c'}
a
或
{
'c', 'a', 'b'}
c
等
可以看到,如果集合中’a’In front then the maximum value is ’a’,如果’c’In front then the maximum value is ’c’.
當然,If we pass in a list,則是確定的’c’:
chars = ['b', 'c', 'a', 'a', 'c']
print(max(chars, key=chars.count))
輸出:
c