Reputation: 5708
I have a group of records that I am wanting to group together on two separate fields. Each record is a python dictionary. One of the fields is a date value and the other is a number field. I.e.,:
h = [{'date': 20170728, 'group': 121, ...},
{'date': 20170729, 'group': 131, ...},
...]
Now, if I wanted to group certain groups together, say any group that is in [123, 134, 145] but has the same date, group them together, but every other group gets grouped together by themselves, how would I accomplish this?
I'm using the following code:
grouped_list = []
for date, items in groupby(h, key=itemgetter('date'):
g = list(items)
grouped_list.append(g)
The output that I'm looking for is the following:
grouped_list = [
[records that have a distinct date value and group],
[records that have a distinct date but are in the group [123, 134, 145],
etc.]
The records in groups 123, 134, and 145 should not have their own respective lists in the grouped_list. They should be grouped together in a list.
Upvotes: 1
Views: 661
Reputation: 3187
You can write a custom function to compute the keys to group your records, something like this:
from itertools import groupby
records = [
{'date': 20170728, 'group': 121},
{'date': 20170729, 'group': 131},
{'date': 20170729, 'group': 134},
{'date': 20170729, 'group': 145},
]
grouped_groups = [123, 134, 145]
def compute_groupby_key(entry):
return "%d-%d" % (
entry['date'],
grouped_groups[0] if entry['group'] in grouped_groups else entry['group']
)
grouped_records = [list(entries) for key, entries in groupby(records, compute_groupby_key)]
here grouped_records
contains:
[
[{'date': 20170728, 'group': 121}],
[{'date': 20170729, 'group': 131}],
[{'date': 20170729, 'group': 134}, {'date': 20170729, 'group': 145}]]
]
Upvotes: 1