diff options
author | Raymond Hettinger <python@rcn.com> | 2004-01-20 20:04:40 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-01-20 20:04:40 (GMT) |
commit | 734fb5724ffd005fd13b1d7512b479554c5c9efd (patch) | |
tree | b44ea37db06198e91b92ec71c0160c36113f03c3 /Lib | |
parent | 57cb68fe93ab99e9dc3fb702755cfd8d914b6834 (diff) | |
download | cpython-734fb5724ffd005fd13b1d7512b479554c5c9efd.zip cpython-734fb5724ffd005fd13b1d7512b479554c5c9efd.tar.gz cpython-734fb5724ffd005fd13b1d7512b479554c5c9efd.tar.bz2 |
Add a Guido inspired example for groupby().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_itertools.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 31b1b7c..fe49f75 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -677,6 +677,20 @@ Samuele 2 ['b', 'd', 'f'] 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)) |