What is the criterion for maximum key?
>>> a = {'b':[1, 2, 3], 'c':[2, 3, 4, 5], 'd':[1234, 6789, 23456]} >>> max(a) 'd' >>> a = {'b':[1, 2, 3], 'c':[2, 3, 4, 5], 'd':[1234, 6789, 23456], 'v':[1, 2, 3, 4, 5, 6, 7]} >>> max(a) 'v' What is the criterion for maximum key?
>>> a = {'b':[1, 2, 3], 'c':[2, 3, 4, 5], 'd':[1234, 6789, 23456]} >>> max(a) 'd' >>> a = {'b':[1, 2, 3], 'c':[2, 3, 4, 5], 'd':[1234, 6789, 23456], 'v':[1, 2, 3, 4, 5, 6, 7]} >>> max(a) 'v' The max function accepts any iterated object, returns the maximum of its elements. If you iterate a dictionary with for , for example, you get a sequence of its keys, so the max function will return the maximum of the keys. For lines in python, the one that is later in the lexicographical (alphabetical) order is considered the most. In the first case, the maximum key will be 'd' , in the second 'v' .
In general, for objects for which you need to find the maximum (without using the key argument of the max function, about it below), a more-less ratio should be defined. For your classes, you must at least define the __lt__() method:
>>> class A(): ... def __init__(self, value): ... self.value = value ... ... def __lt__(self, other): ... return self.value > other.value # ΠΠ½Π°ΠΊ Π±ΠΎΠ»ΡΡΠ΅ Π²ΠΌΠ΅ΡΡΠΎ ΠΌΠ΅Π½ΡΡΠ΅ Π²ΡΡΠ°Π²Π»Π΅Π½ Π½Π°ΠΌΠ΅ΡΠ΅Π½Π½ΠΎ ... ... def __repr__(self): ... return 'A(%r)' % self.value >>> max([A(1), A(2), A(3)]) A(1) To get the maximum on some basis (not the way the comparison is determined for specific objects), you can use the key argument of the max function. Through this argument, you can pass a function that will return the parameter by which you want to make a comparison. A typical example is to find the maximum length:
s = ['abc', 'ab', 'def', 'dead'] print(max(s)) # ΠΌΠ°ΠΊΡΠΈΠΌΡΠΌ ΠΏΠΎ Π°Π»ΡΠ°Π²ΠΈΡΡ - 'def' print(max(s, key=len)) # ΠΌΠ°ΠΊΡΠΈΠΌΡΠΌ ΠΏΠΎ Π΄Π»ΠΈΠ½Π΅ - 'dead' Another fairly typical case is a list of tuples, you need to select a maximum of one of the fields of the tuple:
s = [(1, 10), (2, 9), (3, 8), (4, 7), (5, 6), (6, 5), (7, 4), (8, 3), (9, 2), (10, 1)] # Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΏΠΈΡΠΊΠ° ΠΏΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΊΠΎΡΡΠ΅ΠΆΠ°: print(max(s, key=lambda x: x[0])) # Π²ΡΠ²Π΅Π΄Π΅Ρ (10, 1) # Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΏΠΈΡΠΊΠ° ΠΏΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π²ΡΠΎΡΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΊΠΎΡΡΠ΅ΠΆΠ°: print(max(s, key=lambda x: x[1])) # Π²ΡΠ²Π΅Π΄Π΅Ρ (1, 10) max(a,b,c) and so it works. - jfs__lt__ , and not what max () can accept lists. You should use max(a,b,c) , not max([a,b,c]) in Python. It is also not necessary to __lt__ method, for example: max([1], "2", key=len) , although the [1] < "2" comparison is not defined (TypeError). - jfsmax(a,b,c) should be used, not max([a,b,c]) in Python." - perhaps more preferable, but certainly not strictly "follows . " Concerning the key argument, I added the answer. - insolor__lt__ wrong. - jfsSource: https://ru.stackoverflow.com/questions/599515/
All Articles
max({'Ρ': 0, 'Ρ': 0}) == 'Ρ'- andreymal