Simple map/reduce utility function for data analysis

Raymond Hettinger python at rcn.com
Tue Apr 26 14:12:33 EDT 2011


On Apr 25, 7:42 pm, Paul Rubin <no.em... at nospam.invalid> wrote:
> Raymond Hettinger <pyt... at rcn.com> writes:
> > Here's a handy utility function for you guys to play with:
> >    http://code.activestate.com/recipes/577676/
>
> Cute, but why not use collections.defaultdict for the return dict?
> Untested:

My first draft had a defaultdict but that implementation detail would
get exposed to the user unless the return value was first coerced to a
regular dict.  Also, I avoided modern python features so the code
would run well on psyco and so that it would make sense to beginning
users.


> Untested:
>   d = defaultdict(list)
>   for key,value in ifilter(bool,imap(mapper, data)):
>      d[key].append(value)
>   ...

Nice use of itertools.  FWIW, ifilter() will accept None for the first
argument -- that's a bit faster than using bool().


Raymond



More information about the Python-list mailing list