Reputation: 11197
here is the basic code:
from my_parser import data
from itertools import groupby
from collections import Counter
def grades(date=datetime.now().strftime('%Y%m')):
this_month = self.data.key_by('GRADE').pop(date) # this gives a list of this month
monthly = dict(groupby(this_month, lambda k: make_letter(k.GRADE))) # group it by letter grade
c = [{
'month': k,
'total': len(x)
} for k,x in monthly.iteritems()]
however, the dictionary returns like so:
A : <itertools._grouper object at 0x1033f0c90>
C : <itertools._grouper object at 0x1033f0c50>
is it possible to get it to return more like this, or an alternate tool to do the same concept?
{
A : [ ... ]
B : [ ... ]
}
Upvotes: 2
Views: 59
Reputation: 56674
Instead of
monthly = dict(groupby(this_month, lambda k: make_letter(k.GRADE))) # group it by letter grade
try
monthly = {
val:list(items)
for val,items in
groupby(this_month, lambda k: make_letter(k.GRADE))
}
Edit: You may need to pre-sort this_month such that grades are in ascending order (or at least such that all grades corresponding to a given letter appear together).
Upvotes: 2