With the data below, I'm trying to unfold a dictionary that contains a list of dictionaries, and then group each key with the corresponding values of the other dictionaries together. For example:
result = {
'themes' : [{
'a' : 'orange',
'b' : 6,
'c' : 'neutral',
'd' : 6,
'e' : 0.24
}, {
'a' : 'banana',
'b' : 6,
'c' : 'neutral',
'd' : 6,
'e' : 0.16
}, {
'a' : 'phone',
'b' : 5,
'c' : 'neutral',
'd' : 5,
'e' : 0.02
}
]
}
...should become something along these lines:
themes={'a' : ['orange','banana', 'phone']}
count={'b' : [6,6,5]}
s_score={'c' : [neutral, neutral, neutral]}
...and so on.
I've looked here, here, and here among other places, but couldn't find something close enough to what I want to do. This came pretty close, but it's checking for at least one or more common values, whereas mine should group common keys. I know I can separate the outer key from the values like this:
>>>(k, v), = result.items()
>>>k
>>>'themes'
>>>v
>>>[{
'a' : 'orange',
'b :6,
'c' : 'neutral',
'd' : 6,
'e' : 0.24
}, {
'a' : 'banana',
'b' : 6,
'c' : 'neutral',
'd' : 6,
'e' : 0.16
}, {
'a' : 'phone',
'b' : 5,
'c' : 'neutral',
'd' : 5,
'e' : 0.02
}
]
but how do I get the v list of dictionaries to the way I described? Do I have to convert them to sets first?
To make my intention clear, my ultimate goal is iterate through the list of values of the keys that I want to keep, so I can enter them into their respective columns in my fairly basic flask-sqlalchemy SQLite database. So in the end I'll be able to query and get them displayed as html:
+-----------------+----------+----------+-------+
| a | b | c | d |
+-----------------+----------+----------+-------+
| orange | 2.4 | neutral | 6 |
| banana | 1.6 | neutral | 6 |
+-----------------+----------+----------+-------+