[Python-Dev] tally (and other accumulators)

Scott David Daniels Scott.Daniels at Acm.Org
Wed Apr 5 21:56:14 CEST 2006


Jess Austin wrote:
> Alex wrote:
>> On Apr 4, 2006, at 10:53 PM, Jess Austin wrote:
>>> Alex wrote:
>>>> import collections
>>>> def tally(seq):
>>>>      d = collections.defaultdict(int)
>>>>      for item in seq:
>>>>          d[item] += 1
>>>>      return dict(d)
[Jess again]
>>> def tally(seq):
>>>     return dict((group[0], len(tuple(group[1])))
>>>                 for group in itertools.groupby(sorted(seq)))

>>> In general itertools.groupby() seems like a very clean way to do this
>>> sort of thing, whether you want to end up with a dict or not.  I'll go
>>> so far as to suggest that the existence of groupby() obviates the
>>> proposed tally().  Maybe I've just coded too much SQL and it has  
>>> warped my brain...

> You're right in that it won't raise an exception on an iterator, but the
> sorted() means that it's much less memory efficient than your version
> for iterators.  Another reason to avoid sorted() for this application,
> besides the big-O.  Anyway, I still like groupby() for this sort of
> thing, with the aforementioned caveats.  Functional code seems a little
> clearer to me, although I realize that preference is not held
> universally.

However, sorted requires ordering.  Try seq = [1, 1j, -1, -1j] * 5
Alex's tally works, but yours does not.

-- 
-- Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-Dev mailing list