[Python-checkins] python/dist/src/Lib/test test_itertools.py, 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/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv20952/lib/test

Modified Files:
	test_itertools.py 
Log Message:
Add a Guido inspired example for groupby().

Index: test_itertools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_itertools.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** test_itertools.py	17 Dec 2003 20:43:32 -0000	1.27
--- test_itertools.py	20 Jan 2004 20:04:31 -0000	1.28
***************
*** 678,681 ****
--- 678,695 ----
  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]
+ 
  >>> def take(n, seq):
  ...     return list(islice(seq, n))





More information about the Python-checkins mailing list