diff options
author | Raymond Hettinger <python@rcn.com> | 2003-09-08 23:58:40 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-09-08 23:58:40 (GMT) |
commit | a098b33c9346ae8c8891982cd733d5d9dcdae2ec (patch) | |
tree | 0f79aae580e1af9335a3b0f9fc77d27db5bbb978 /Lib/test/test_itertools.py | |
parent | 9d50d91e77974c233c1c136f1a7a8ce7d53cea09 (diff) | |
download | cpython-a098b33c9346ae8c8891982cd733d5d9dcdae2ec.zip cpython-a098b33c9346ae8c8891982cd733d5d9dcdae2ec.tar.gz cpython-a098b33c9346ae8c8891982cd733d5d9dcdae2ec.tar.bz2 |
Add an example to address a common question of how to split iterators.
Diffstat (limited to 'Lib/test/test_itertools.py')
-rw-r--r-- | Lib/test/test_itertools.py | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 057b576..8ab4cea 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -487,6 +487,9 @@ Martin Walter Samuele +>>> def take(n, seq): +... return list(islice(seq, n)) + >>> def enumerate(iterable): ... return izip(count(), iterable) @@ -539,12 +542,26 @@ Samuele ... result = result[1:] + (elem,) ... yield result ->>> def take(n, seq): -... return list(islice(seq, n)) +>>> def tee(iterable): +... "Return two independent iterators from a single iterable" +... def gen(next, data={}, cnt=[0]): +... dpop = data.pop +... for i in count(): +... if i == cnt[0]: +... item = data[i] = next() +... cnt[0] += 1 +... else: +... item = dpop(i) +... yield item +... next = iter(iterable).next +... return (gen(next), gen(next)) This is not part of the examples but it tests to make sure the definitions perform as purported. +>>> take(10, count()) +[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + >>> list(enumerate('abc')) [(0, 'a'), (1, 'b'), (2, 'c')] @@ -590,8 +607,17 @@ False >>> dotproduct([1,2,3], [4,5,6]) 32 ->>> take(10, count()) -[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +>>> def irange(start, stop): +... for i in range(start, stop): +... yield i + +>>> x, y = tee(irange(2,10)) +>>> list(x), list(y) +([2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9]) + +>>> x, y = tee(irange(2,10)) +>>> zip(x, y) +[(2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)] """ |