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' 
  • maximum key? - pavel
  • What is the maximum criterion? - lalalala
  • Well, for the lines, the exact ratio is more less ... - pavel
  • For characters - alphabetical order. - Sergey Gornostaev
  • one
    @SergeyGornostaev max({'я': 0, 'Ρ‘': 0}) == 'Ρ‘' - andreymal

1 answer 1

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) 
  • The brackets are not needed at the end: max(a,b,c) and so it works. - jfs
  • @jfs, yes, but it will be a function call not with an iterated argument, but with several arguments. - insolor
  • You illustrate __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). - jfs
  • " max(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
  • 1- "There should be one-- and 2 only- Obvious way to do it" 2- I didn’t add a comment to the fact that the answer is not complete β€” I would generally delete everything from the words β€œin general” (more - not always better), but to the fact that the explanation about __lt__ wrong. - jfs