问题
I have a dictionary like below:
mydict = {'a' : 'apple',
'b' : 'bobb',
'c' : {
'd' : 'dog'
},
'e' : 'dog'
}
Suppose I want to find all the keys with value 'dog
'In case of nesting, the keys should be separated by a dot.
So the output should be a list ['e', 'c.d']
Now if I write below code in python 3, it only outputs 'e
'.
print(list(mydict.keys())[list(mydict.values()).index('dog')])
How can I get the nested keys?
回答1:
You can use a recursion function like following:
def find_key(mydict, pre=tuple()):
for key, value in mydict.items():
if isinstance(value, dict):
yield from find_key(value, pre=pre+(key,))
elif value == 'dog':
if pre:
yield '.'.join(pre + (key,))
else:
yield key
test:
In [23]: list(find_key(mydict))
Out[23]: ['e', 'c.d']
In [26]: mydict = {'a' : 'apple',
'b' : 'bobb',
'c' : {
'd' : 'dog'
},
'e' : 'dog',
'k':{'f':{'c':{'x':'dog'}}}}
In [27]:
In [27]: list(find_key(mydict))
Out[27]: ['k.f.c.x', 'e', 'c.d']
回答2:
You can use recursion (although @Kasramvd beat me to it, here is a non-generator version for good measure):
>>> def find_key(value, d, level=None, acc=None):
... acc = acc if acc is not None else []
... level = level if level is not None else []
... for k,v in d.items():
... if isinstance(v, dict):
... find_key(value, v, level=[*level, k], acc=acc)
... elif v == value:
... acc.append('.'.join([*level,k]))
... return acc
...
>>>
>>> find_key('dog', mydict)
['c.d', 'e']
And with Kasramvd's messier test-case:
>>> mydict = {'a' : 'apple',
... 'b' : 'bobb',
... 'c' : {
... 'd' : 'dog'
... },
... 'e' : 'dog',
... 'k':{'f':{'c':{'x':'dog'}}}}
>>> find_key('dog', mydict)
['c.d', 'e', 'k.f.c.x']
回答3:
You can also use recursion with a list comprehension:
mydict1 = {'a' : 'apple',
'b' : 'bobb',
'c' : {
'd' : 'dog'
},
'e' : 'dog'
}
mydict = {'a' : 'apple',
'b' : 'bobb',
'c' : {
'd' : 'dog'
},
'e' : 'dog',
'k':{'f':{'c':{'x':'dog'}}}}
s = [mydict1, mydict]
def get_val(d, target='dog'):
return reduce(lambda x, y:([x] if isinstance(x, str) else x)+([y] if isinstance(y, str) else y), list(filter(None, [a if b == target else "{}.{}".format(a, get_val(b)) if isinstance(b, dict) else None for a, b in d.items()])))
new_s = list(map(get_val, s))
Output:
[['c.d', 'e'], ['c.d', 'e', 'k.f.c.x']]
回答4:
Here you go :
mydict = {'a' : 'apple',
'b' : 'bobb',
'c' : {
'd' : 'dog'
},
'e' : 'dog',
'k':{'f':{'c':{'x':'dog'}}},
'p':{'r':{'h':{'m':{'n':{'o':{'a':{'b':{'t':{'z':{'a':{'b':{'c':{'d':{'e':{'f':{'g':'dog'}}}}}}}}}}}}}}}}
}
your_word='dog'
result = []
for key,value in mydict.items():
if value==your_word:
result.append(key)
elif isinstance(value,dict):
def recursive_approach(my_dict, nested_dict=key):
for i, j in my_dict.items():
if isinstance(j, dict):
nested_dict = "{}.{}".format(nested_dict, i)
return recursive_approach(j, nested_dict)
else:
if nested_dict == '':
if j == your_word:
result.append(i)
else:
if j == your_word:
result.append("{}.{}".format(nested_dict, i))
recursive_approach(value)
print(result)
output:
['p.r.h.m.n.o.a.b.t.z.a.b.c.d.e.f.g', 'k.f.c.x', 'e', 'c.d']
来源:https://stackoverflow.com/questions/48276165/python-find-all-keys-with-a-value