Looping in dictionaries

0

I have a problem with dictionaries in python.

How do I get the key to a dictionary from the value?

That is, if I had a dictionary of the form;

d={"key1":
  {"key2":
  {"e":5,"f":8,....,"z":10}}}

and I want to know which key contains the value 50.

    
asked by Klever Puma 25.01.2018 в 14:36
source

1 answer

0
d={"key1":{"key2":{"e":5,"f":8,"z":10}}}

def buscar(d, criterio, p = ""):
    for k, v in d.items():
        if isinstance(v, dict):
            buscar(v, criterio, "{} > {}".format(p, k))
        elif v == criterio:
            print("{} > {} : {}".format(p, k, v))

buscar(d, 10)

The result would be: > key1 > key2 > z: 10

Greetings

    
answered by 25.01.2018 в 15:01