Avec python pour trouver la valeur maximum dans une liste de nombre il existe la fonction "build-in": max. Un exemple simple d'utilisation:
>>> a = [3,8,3,5,9,1,4]
>>> max(a)
9
Pour trouver l'indice (ou les indices sir la valeur maximum est présente plusieurs fois dans la liste) vous pouvez utiliser l'approche suivante:
>>> a = [3,8,3,5,9,1,4]
>>> m = max(a)
>>> [i for i, j in enumerate(a) if j == m]
[4]
Note: dans le cas où la liste comprend des nombres et des "strings", ces derniers sont toujours considérés plus grand que des nombres (voir How does Python compare string and int?). Illustration:
>>> a = [3,8,3,5,9,1,4,'a']
>>> max(a)
'a'
Recherches associées
Liens | Site |
---|---|
Built-in Functions: max() | Python Doc |
Python List max() Method | Tutorial Point |
How to find positions of the list maximum? | stackoverflow |
Python Finding Index of Maximum in List | stackoverflow |
How to return the maximum element of a list in Python? | stackoverflow |
Python: Getting the max value of y from a list of objects | stackoverflow |
Pythonic way to find maximum value and its index in a list? | stack overflow |
Python's most efficient way to choose longest string in list? | stack overflow |
How does Python compare string and int? | stack overflow |