python get dict key by value
- python keys
- python dict search key,
- find in dictionary python
Utilisant list.index()
La méthode index()
renvoie l’index de la valeur correspondante dans une liste. Vous trouverez ci-dessous une implémentation de l’utilisation de la méthode index()
pour récupérer la clé du dictionnaire à l’aide de la valeur.
myDict ={"ruby":1, "javascript":2, "go":3} keys = list(myDict.keys()) vals = list(myDict.values()) position = vals.index(1) print(keys[position]) position = vals.index(2) print(keys[position]) print(list(myDict.keys())[list(myDict.values()).index(3)])
# ruby # javascript # go
find a key in a dictionary python
Utilisant dict.item()
Nous pouvons également récupérer la clé d’une valeur en faisant correspondre toutes les valeurs, puis imprimer la clé correspondante à la valeur donnée.
def getKey(val): for key, value in myDict.items(): if val == value: return key return "oops" myDict ={"javascript":2, "go":1, "cpp":3} print(getKey(2)) print(getKey(3)) print(getKey(1))
how to get the key of a value in python
# javascript # cpp # go
python get dict key by value
- python keys
- python dict search key,
- find in dictionary python
Utilisant list.index()
La méthode index()
renvoie l’index de la valeur correspondante dans une liste. Vous trouverez ci-dessous une implémentation de l’utilisation de la méthode index()
pour récupérer la clé du dictionnaire à l’aide de la valeur.
myDict ={"ruby":1, "javascript":2, "go":3} keys = list(myDict.keys()) vals = list(myDict.values()) position = vals.index(1) print(keys[position]) position = vals.index(2) print(keys[position]) print(list(myDict.keys())[list(myDict.values()).index(3)])
# ruby # javascript # go
find a key in a dictionary python
Utilisant dict.item()
Nous pouvons également récupérer la clé d’une valeur en faisant correspondre toutes les valeurs, puis imprimer la clé correspondante à la valeur donnée.
def getKey(val): for key, value in myDict.items(): if val == value: return key return "oops" myDict ={"javascript":2, "go":1, "cpp":3} print(getKey(2)) print(getKey(3)) print(getKey(1))
how to get the key of a value in python
# javascript # cpp # go