itertools.groupby
Raymond Hettinger
python at rcn.com
Mon May 28 01:29:58 EDT 2007
On May 27, 8:28 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> I use the module all the time now and it is great.
Thanks for the accolades and the great example.
FWIW, I checked in a minor update to the docs:
+++ python/trunk/Doc/lib/libitertools.tex Mon May 28 07:23:22 2007
@@ -138,6 +138,13 @@
identity function and returns the element unchanged. Generally,
the
iterable needs to already be sorted on the same key function.
+ The operation of \function{groupby()} is similar to the \code{uniq}
filter
+ in \UNIX{}. It generates a break or new group every time the value
+ of the key function changes (which is why it is usually necessary
+ to have sorted the data using the same key function). That
behavior
+ differs from SQL's GROUP BY which aggregates common elements
regardless
+ of their input order.
+
The returned group is itself an iterator that shares the underlying
iterable with \function{groupby()}. Because the source is shared,
when
the \function{groupby} object is advanced, the previous group is no
@@ -147,6 +154,7 @@
\begin{verbatim}
groups = []
uniquekeys = []
+ data = sorted(data, key=keyfunc)
for k, g in groupby(data, keyfunc):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append(k)
Raymond
More information about the Python-list
mailing list