Eric.P
Eric.P

Reputation: 159

Is there another way to extract info with complex/unstructured nested dict format in Python?

Suppose I have an unstructured nested dict as following:

{
'A_brand': {'score1': {'A': 13, 'K': 50}},
'B_brand': {'before_taste': {'score2': {'A': 43, 'D': 23}}, 'after_taste': {'score3': {'H': 36, 'J': 34}}},
'Score4': {'G': 2, 'W': 19}
}

How can I get/show the info like: Which letter get the highest score for each scores? like:

{'key':'value',
'A_brand/score1':'K',
'B_brand/before_taste/score2':'A',
'B_brand/after_taste/score3':'H',
'Score4':'W'}

What I did was dummies way which I created a new dict and accessed into each path, sorted them by values and selected first one item, then added it into the new dict. For example:

new_csv={'key':'value'}

first=data['A_brand']['before_lunch_break']['score1']
first_new=sorted(first.items(),key=lambda x: x[1],reverse=True)
new_csv['A_brand/score']=first_new[0][0]

second=data['B_brand']['before_taste']['score2']
second_new=sorted(second.items(),key=lambda x: x[1],reverse=True)
new_csv['B_brand/before_taste/score2']=second_new[0][0]

...

I'm wondering if there is any faster or automatic ways to do that?

Upvotes: 1

Views: 36

Answers (1)

Ajax1234
Ajax1234

Reputation: 71461

You can use a generator with recursion:

data = {'A_brand': {'score1': {'A': 13, 'K': 50}}, 'B_brand': {'before_taste': {'score2': {'A': 43, 'D': 23}}, 'after_taste': {'score3': {'H': 36, 'J': 34}}}, 'Score4': {'G': 2, 'W': 19}}
def get_max(d, c = []):
   for a, b in d.items():
     if all(not isinstance(i, dict) for i in b.values()): 
        yield ('/'.join(c+[a]), max(b, key=lambda x:b[x]))
     else:
        yield from get_max(b, c+[a])

print(dict(get_max(data)))

Output:

{'A_brand/score1': 'K', 'B_brand/before_taste/score2': 'A', 'B_brand/after_taste/score3': 'H', 'Score4': 'W'}

Upvotes: 1

Related Questions