itertools.groupby

Gordon Airporte JHoover at fbi.gov
Mon May 28 23:02:31 EDT 2007


Paul Rubin wrote:

> It chops up the iterable into a bunch of smaller ones, but the total
> size ends up the same.  "Telescope", "compact", "collapse" etc. make
> it sound like the output is going to end up smaller than the input.

Good point... I guess I was thinking in terms of the number of iterators 
being returned being smaller than the length of the input, and ordered 
relative to the input - not about the fact that the iterators contain 
all of the objects.


> There is also a dirty secret involved <wink>, which is that the
> itertools functions (including groupby) are mostly patterned after
> similarly named functions in the Haskell Prelude, which do about the
> same thing.  They are aimed at helping a similar style of programming,
> so staying with similar names IMO is a good thing.

Ah - those horrible, intolerant Functionalists. I dig ;-).

> But that is what groupby does, except its notion of uniqueness is
> limited to contiguous runs of elements having the same key.

"itertools.groupby_except_the_notion_of_uniqueness_is_limited_to-
_contiguous_runs_of_elements_having_the_same_key()" doesn't have much of 
a ring to it. I guess this gets back to documentation problems, because 
the help string says nothing about this limitation:

'''
class groupby(__builtin__.object)
  |  groupby(iterable[, keyfunc]) -> create an iterator which returns
  |  (key, sub-iterator) grouped by each value of key(value).
  |
'''

"Each" seems to imply uniqueness here.



More information about the Python-list mailing list