diff options
author | Stan Seibert <stan@mtrr.org> | 2011-08-21 11:02:22 -0400 |
---|---|---|
committer | Stan Seibert <stan@mtrr.org> | 2011-08-21 11:02:22 -0400 |
commit | 64c1b48541a3582b797b39e7c88304a03205a04d (patch) | |
tree | 3fd0e2991a543a21cfd896ee0d0d59781c835436 /itertoolset.py | |
parent | 6f025f9181500fd8f687d7873898b5401bc4295e (diff) | |
download | chroma-64c1b48541a3582b797b39e7c88304a03205a04d.tar.gz chroma-64c1b48541a3582b797b39e7c88304a03205a04d.tar.bz2 chroma-64c1b48541a3582b797b39e7c88304a03205a04d.zip |
Repeating iterator that returns the item from the parent iterator multiple times before moving to the next element.
Diffstat (limited to 'itertoolset.py')
-rw-r--r-- | itertoolset.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/itertoolset.py b/itertoolset.py index 314ba0b..eb96e74 100644 --- a/itertoolset.py +++ b/itertoolset.py @@ -1,5 +1,21 @@ from itertools import * import collections +from copy import deepcopy + +def repeating_iterator(i, nreps): + '''Returns an iterator that emits each element of `i` multiple times specified by `nreps`. + The length of this iterator is the lenght of `i` times `nreps`. This iterator + is safe even if the item consumer modifies the items. + + >>> list(repeating_iterator('ABCD', 3) + ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D'] + >>> list(repeating_iterator('ABCD', 1) + ['A', 'B', 'C', 'D'] + ''' + for item in i: + for counter in xrange(nreps): + yield deepcopy(item) + def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" |