I have records returned from a database that look like this:
region month_taken total_att num_classes
Colorado 2013-01-01 00:00:00.000 78485 4648
Colorado 2013-02-01 00:00:00.000 71769 4162
Midwest 2013-01-01 00:00:00.000 110508 7101
Midwest 2013-02-01 00:00:00.000 103545 6410
I'm attempting to get them into lists as so:
Total_att:
[{"data": [78485, 71769], "name": "Colorado"}, {"data": [110508, 103545], "name": "Midwest"}]
num_classes:
[{"data": [4648, 4162], "name": "Colorado"}, {"data": [7101, 6410], "name": "Midwest"}]
I discovered itertools.groupby which does what I want, but I'm having difficulty doing it with more than one list of values (for lack of a better term).
totalResults = []
for key, location in groupby(rows, lambda k: k[0]):
totalRow = dict()
totalRow['name'] = key
totalRow['data'] = [x[2] for x in location]
totalResults.append(totalRow)
Great, that gets me my total_att list, but then I do an entire additional groupby loop to create the "num_classes" list, which seems ridiculous. I saw this in the docs, but honestly I'm not quite sure what it means or how to handle my problem if I converted it to a list:
The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list:
So, how can I create my lists without doing multiple for key, location in groupby(rows, lambda k: k[0]):?
I hope that is clear, but am happy to provide more information as necessary.
lambdafunction, you should useoperator.itemgetter(0). (If you use gnibbler's answer, then you can store the itemgetter into a variable and use it twice to save creating the same thing twice).itertools.teeto create 2 independent iterators from a single iterable.