diff options
-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" |