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

Popular posts from this blog

python - jinja2: TemplateSyntaxError: expected token ',', got 'string' -

Qt4: how to send QString inside a struct via QSharedMemory -

node.js - NodeJS remote terminal to Dropbear OpenWRT-Server -