Pour trouver la plus petite valeur d'une liste on peut utiliser min():
>>> l = [4,7,1,4,1,6,9]
>>> min(l)
1
qui donne ici 1. Si on veut connaitre la position dans la liste de la plus petite valeur on peut utiliser index()
>>> l.index(min(l))
2
cependant la méthode index ne donne la position que de la première valeur minimum trouvée. Si comme dans notre exemple il existe plusieurs indices avec une valeur minimum on peut alors utiliser les lists comprehensions:
>>> indices = [i for i, x in enumerate(l) if x == min(l)]
>>> indices
[2, 4]
Références
Liens | Site |
---|---|
Python List min() Method | tutorialspoint.com |
How to find all occurrences of an element in a list? | stackoverflow |
Find the smallest number in a python list and print the position | stackoverflow |
Python List index() Method | tutorialspoint |
Finding the index of an item given a list containing it in Python | stackoverflow |
Getting the index of the returned max or min item using max()/min() on a list | stackoverflow |
Python: finding lowest integer | stackoverflow |
Find the minimum value in a python list | stackoverflow |
How to return all the minimum indices in numpy | stackoverflow |
numpy.argmin | scipy doc |