[Tutor] List processing question - consolidating duplicate entries
Kent Johnson
kent37 at tds.net
Thu Nov 29 19:57:53 CET 2007
Richard Querin wrote:
> 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.
You must be using an older version of Python, the ability to pass
multiple arguments to itemgetter was added in 2.5. Meanwhile it's easy
enough to define your own:
def make_key(item):
return (item[:4])
and then specify key=make_key.
BTW when you want help with an error, please copy and paste the entire
error message and traceback into your email.
> 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]).
No, it returns one function that will return a tuple of values.
> Should I only be calling itemgetter with whatever element (0 to 3) that
> I want to group the items by?
If you do that it will only group by the single item you specify.
groupby() doesn't sort so you should also sort by the same key. But I
don't think that is what you want.
Kent
More information about the Tutor
mailing list