Simple map/reduce utility function for data analysis

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Apr 25 22:52:17 EDT 2011


On Mon, 25 Apr 2011 16:48:42 -0700, Raymond Hettinger wrote:

> Here's a handy utility function for you guys to play with:
> 
>     http://code.activestate.com/recipes/577676/

Nice. 

That's similar to itertools.groupby except that it consolidates all the 
equal key results into one list, instead of in consecutive runs.

Also groupby returns iterators instead of lists, which makes it a PITA to 
work with. map_reduce is much more straightforward to use.

Example given in the code:

>>> map_reduce(range(30), even_odd)
{0: [10, 12, 14, 16, 18, 20], 1: [11, 13, 15, 17, 19]}

>>> [(key[0], list(group)) for key,group in groupby(range(30), even_odd) 
if key is not None]
[(0, [10]), (1, [11]), (0, [12]), (1, [13]), (0, [14]), (1, [15]), (0, 
[16]), (1, [17]), (0, [18]), (1, [19]), (0, [20])]


So... when can we expect map_reduce in the functools module? :)



-- 
Steven



More information about the Python-list mailing list