python - One line repeating counter with itertools? -
i want write infinte generator using itertools combines count
, repeat
iterators, repeating each number n
times before incrementing. there more concise way came with?
from itertools import * def repeating_counter(times): c = count() while true: x in repeat(next(c), times): yield x >>> rc = repeating_counter(2) >>> next(rc) 0 >>> next(rc) 0 >>> next(rc) 1 >>> next(rc) 1
use integer division!
def repeating_counter(times): return (x // times x in count())
Comments
Post a Comment