On Nov 27, 2007 5:40 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > > This is a two-liner using itertools.groupby() and operator.itemgetter: > > data = [['Bob', '07129', 'projectA', '4001',5], > ['Bob', '07129', 'projectA', '5001',2], > ['Bob', '07101', 'projectB', '4001',1], > ['Bob', '07140', 'projectC', '3001',3], > ['Bob', '07099', 'projectD', '3001',2], > ['Bob', '07129', 'projectA', '4001',4], > ['Bob', '07099', 'projectD', '4001',3], > ['Bob', '07129', 'projectA', '4001',2] > ] > > import itertools, operator > for k, g in itertools.groupby(sorted(data), key=operator.itemgetter(0, > 1, 2, 3)): > print k, sum(item[4] for item in g) >
I'm trying to understand what's going on in the for statement but I'm having troubles. The interpreter is telling me that itemgetter expects 1 argument and is getting 4. I understand that groupby takes 2 parameters the first being the sorted list. The second is a key and this is where I'm confused. The itemgetter function is going to return a tuple of functions (f[0],f[1],f[2],f[3]). Should I only be calling itemgetter with whatever element (0 to 3) that I want to group the items by? I'm almost getting this but not quite. ;) RQ
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor