# [Python-checkins] python/dist/src/Doc/lib libitertools.tex, 1.27, 1.28

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Tue Jan 20 15:04:42 EST 2004

Update of /cvsroot/python/python/dist/src/Doc/lib
In directory sc8-pr-cvs1:/tmp/cvs-serv20952/doc/lib

Modified Files:
libitertools.tex
Log Message:
Add a Guido inspired example for groupby().

Index: libitertools.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libitertools.tex,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** libitertools.tex	18 Dec 2003 13:28:35 -0000	1.27
--- libitertools.tex	20 Jan 2004 20:04:40 -0000	1.28
***************
*** 407,410 ****
--- 407,423 ----
3 ['g']

+ # Find runs of consecutive numbers using groupby.  The key to the solution
+ # is differencing with a range so that consecutive numbers all appear in
+ # same group.
+ >>> data = [ 1,  4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
+ >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
+ ...     print map(operator.itemgetter(1), g)
+ ...
+ [1]
+ [4, 5, 6]
+ [10]
+ [15, 16, 17, 18]
+ [22]
+ [25, 26, 27, 28]

\end{verbatim}
***************
*** 412,416 ****
This section shows how itertools can be combined to create other more
powerful itertools.  Note that \function{enumerate()} and \method{iteritems()}
! already have efficient implementations in Python.  They are only included here
to illustrate how higher level tools can be created from building blocks.

--- 425,429 ----
This section shows how itertools can be combined to create other more
powerful itertools.  Note that \function{enumerate()} and \method{iteritems()}
! already have efficient implementations.  They are included here
to illustrate how higher level tools can be created from building blocks.