Grouping data in a list of of dicts
I have a list of dicts that looks like this:
[{TYPE, OBJECT_ID, ACTOR, EXTRA_FIELDS}, ...]
I'd like to go through and aggregate the duplicates of {TYPE, OBJECT_ID}
and make ACTOR a list so:
start with:
[ {'type': 'LOVE', 'obj_id': 1242, 'actor': 'bob', {..}},
{'type': 'LOVE', 'obj_id': 1242, 'actor': 'dave', {..}},
{'type': 'FAV', 'obj_id': 1242, 'actor': 'sam', {..}},
{'type': 'LOVE', 'obj_id': 242, 'actor': 'bob', {..}}]
end up with:
[ {'type': 'LOVE', 'obj_id': 1242, 'actor': ['bob', 'dave'], {..}},
{'type': 'FAV', 'obj_id': 1242, 'actor': ['sam'], {...}},
{'type': 'LOVE', 'obj_id': 242, 'actor': ['bob'], {...}} ]
the EXTRA_FIELDS don't have to be merged, they can just use the data from
one of the items aggregated.
How can I do this in python?
No comments:
Post a Comment