Python itertools - Combining groupby and recipe tools' grouper -
say have following data:
data = [['john', 1], ['ada', 2], ['ada', 3], ['paul', 4], ['paul', 5], ['paul', 6], ['kat', 7], ['kat', 8]]
i can group entries person groupby
:
in [37]: itertools import groupby, izip_longest operator import itemgetter name, g in groupby(data, key=itemgetter(0)): print name, list(g) john [['john', 1]] ada [['ada', 2], ['ada', 3]] paul [['paul', 4], ['paul', 5], ['paul', 6]] kat [['kat', 7], ['kat', 8]]
i can group every 2 entries use recipe tools' grouper. copy/paste reference:
in [38]: def grouper(iterable, n, fillvalue=none): "collect data fixed-length chunks or blocks" # grouper('abcdefg', 3, 'x') --> abc def gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) g in grouper(data, 2): print g (['john', 1], ['ada', 2]) (['ada', 3], ['paul', 4]) (['paul', 5], ['paul', 6]) (['kat', 7], ['kat', 8])
but now, want iterate on data such first element contains john , ada's data, , second element contains paul , kat's data. in other words, combine groupby
, grouper
this:
in [39]: person_iterator = groupby(data, key=itemgetter(0)) group_iterator in grouper(person_iterator, 2): print [(keyvalue[0], list(keyvalue[1])) keyvalue in group_iterator]
but output not expected:
[('john', []), ('ada', [['ada', 2], ['ada', 3]])] [('paul', []), ('kat', [['kat', 7], ['kat', 8]])]
why there empty lists john , paul? how fix it?
a iterator
yielded (group_iterator[1]
) itertools.groupby
exhausted when next iterator
yielded.
you need convert iterators sequences before passing grouper
prevent that:
person_iterator = ((key, list(grp)) key, grp in groupby(data, key=itemgetter(0))) group_iterator in grouper(person_iterator, 2): print [(key, value) key, value in group_iterator]
output:
[('john', [['john', 1]]), ('ada', [['ada', 2], ['ada', 3]])] [('paul', [['paul', 4], ['paul', 5], ['paul', 6]]), ('kat', [['kat', 7], ['kat', 8]])]
Comments
Post a Comment