[^ 1]: Dr. shiracamus teaches us how to write more straightforwardly in the comments.
There is a smart way to write in the comments, so please refer to it.
test= {"w": {"y":{"z":1}},"ww":1, "www":{"yy":1}}
def allkeys(a):
keys = a.keys()
values = a.values()
t = []
for v in values:
if isinstance(v, dict):
t.extend(allkeys(v))
result = [i for i in keys]
result.extend(t)
return result
r = allkeys(test)
print(r)
Execution result
['ww', 'www', 'w', 'yy', 'y', 'z']
https://gist.github.com/lnial/dedf2a98735c2826ca9ef7787bfb18fd
test= {"w": {"y":{"z":1}},"ww":1, "www":{"yy":1}}
def parentnema(k, v):
return k +"."+ v
def allkeys(a):
keys = a.keys()
values = a.values()
t = []
for k in keys:
v = a.get(k)
if isinstance(v, dict):
t.extend(map(lambda z: k + "." + z, allkeys(v)))
result = [i for i in keys]
result.extend(t)
return result
r = allkeys(test)
print(r)
Execution result
['ww', 'www', 'w', 'www.yy', 'w.y', 'w.y.z']
https://gist.github.com/lnial/cd57861fb00a38537dda8974f96a1e1b
Recommended Posts