I have a code that as a result gets a nested dictionary like this:
{'weather': {'cloudy': 'yes',
'rainy': {'wind': {'strong': 'no', 'weak': 'yes'}},
'sunny': {'humidity': {'high': 'no', 'normal': 'yes'}}}}
now I need to "flatten" that to a list of list like this:
[[weather, cloudy, yes], [weather, rainy, wind, strong, no], [weather, rainy, wind, weak, yes], ...]
I've tried many different ways to approach this but just can't get it right, did anyone come across a similar issue?
Edit: someone has requested to see some of my trials, I've tried doing it something like this by turning it into a list:
def change(self, tree):
l = []
for key, value in tree.items():
l.append(key)
if type(value) is dict:
l.extend(change(value))
else:
l.append(value)
return l
this returns:
['weather', 'cloudy', 'yes', 'rainy', 'wind', 'strong', 'no', 'weak', 'yes', 'sunny', 'humidity', 'high', 'no', 'normal', 'yes']
which is not what I need but I'm not sure how to fix it.