]> granicus.if.org Git - python/commitdiff
Explain when groupby() issues a new group.
authorRaymond Hettinger <python@rcn.com>
Mon, 28 May 2007 05:23:22 +0000 (05:23 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 28 May 2007 05:23:22 +0000 (05:23 +0000)
Doc/lib/libitertools.tex

index ac6028b31a747321a35e19ec66b378d4fb926a3c..e2f0f0ef9a975cc7120cfef923a8f5a2546099e5 100644 (file)
@@ -138,6 +138,13 @@ by functions or loops that truncate the stream.
   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 @@ by functions or loops that truncate the stream.
   \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)