itertools-groupby

Sum categories by unique values in list in python

Sum categories by unique values in list in python Question: I have this list: [(‘2023-03-15’, ‘paris’, ‘flight’, 4), (‘2023-03-21’, ‘berlin’, ‘flight’, 2), (‘2023-03-01’, ‘madrid’, ‘drive’, 10), (‘2023-03-04’, ‘madrid’, ‘cycling’, 3), (‘2023-03-08’, ‘rome’, ‘train’, 9), (‘2023-03-11’, ‘amsterdam’, ‘flight’, 5), (‘2023-03-14’, ‘london’, ‘boat’, 1)] How do you reproduce the same list syntax summing similar activities like "flight", …

Total answers: 3

Grouping Python dictionaries in hierarchical form with multiple keys?

Grouping Python dictionaries in hierarchical form with multiple keys? Question: Here is my list of dicts: [{‘subtopic’: ‘kuku’, ‘topic’: ‘lulu’, ‘attachments’: [‘ttt’], ‘text’: ‘abc’}, {‘subtopic’: ‘tutu’, ‘topic’: ‘lulu’, ‘attachments’: [‘pipu’], ‘text’: ‘bubb’}, {‘subtopic’: ‘did’, ‘topic’: ‘lulu’, ‘attachments’: [‘ktop’], ‘text’: ‘gfg’}, {‘subtopic’: ‘polo’, ‘topic’: ‘lulu’, ‘attachments’: [‘vuvu’], ‘text’: ‘prolo’}, {‘subtopic’: ‘ssd’, ‘topic’: ‘lulu’, ‘attachments’: [‘jkjk’], ‘text’: …

Total answers: 2

Python itertools.groupby gives unexpected results when sorting by binary representation

Python itertools.groupby gives unexpected results when sorting by binary representation Question: I am trying to solve a simple problem using itertools.groupby: group the numbers from 0 to 7 according to the number of 1‘s in their binary representation. So I want to produce the mapping {0: [0], 1: [1, 2, 4], 2: [3, 5, 6], …

Total answers: 1

Group emails into TO & CC with itertools.groupby and convert it to a dictionary

Group emails into TO & CC with itertools.groupby and convert it to a dictionary Question: I’d like to group emails by their domain and convert the result into a dictionary. So far I have figured out that itertools.groupby with a custom func will do that. It correctly assigns keys to each value, but when I …

Total answers: 2