[Tutor] MapReduce

Steve Nelson sanelson at gmail.com
Mon Feb 5 23:22:04 CET 2007

On 2/5/07, Steve Nelson <sanelson at gmail.com> wrote:
> What I want to do is now "group" these urls so that repeated urls have
> as their "partner" a lsit of indexes.  To take a test example of the
> method I have in mind:
> def testGrouper(self):
>     """Group occurences of a record together"""
>     test_list = [('fred', 1), ('jim', 2), ('bill', 3), ('jim', 4)]
>     grouped_list = [('fred', 1), ('jim', [2, 4]), ('bill' ,3)]
>     self.assertEqual(myGroup(test_list), grouped_list)


> I would like a clearer, more attractive way of
> making the test pass.  If this can be done in functional style, even
> better.

I now have:

def myGroup(stuff):
  return [(key, map(lambda item: item[1], list(group))) for key, group
in groupby(sorted(stuff), lambda item: item[0] )]

Not sure I fully understand how groupby objects work, nor what a
sub-iterator is, though.  But I more or less understand it.

I understand I could use itemgetter() instead of the lambda...

Can anyone clarify?


More information about the Tutor mailing list