444,002 Members | 1,050 Online
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

# circular iteration

 P: n/a hi, is there a faster way to build a circular iterator in python that by doing this: c=['r','g','b','c','m','y','k'] for i in range(30): print c[i%len(c)] thanks, Flávio Jul 18 '05 #1
4 Replies

 P: n/a Flavio codeco coelho wrote: hi, is there a faster way to build a circular iterator in python that by doing this: c=['r','g','b','c','m','y','k'] for i in range(30): print c[i%len(c)] thanks, Flávio import itertools c=['r','g','b','c','m','y','k'] circ = itertools.cycle(c) for i in range(30): print circ.next(), r g b c m y k r g b c m y k r g b c m y k r g b c m y k r g Jul 18 '05 #2

 P: n/a On 21 Jan 2005 08:31:02 -0800, Flavio codeco coelho wrote: hi, is there a faster way to build a circular iterator in python that by doing this: c=['r','g','b','c','m','y','k'] for i in range(30): print c[i%len(c)] I don''t know if it's faster, but: import itertools c=['r','g','b','c','m','y','k'] for i in itertools.islice(itertools.cycle(c), 30): .... print i -- Cheers, Simon B, si***@brunningonline.net, http://www.brunningonline.net/simon/blog/ Jul 18 '05 #3

 P: n/a "Flavio codeco coelho" wrote: is there a faster way to build a circular iterator in python that by doing this: c=['r','g','b','c','m','y','k'] for i in range(30): print c[i%len(c)] have you benchmarked this, and found it lacking, or are you just trying to optimize prematurely? Jul 18 '05 #4

 P: n/a Simon Brunning wrote: ... is there a faster way to build a circular iterator in python that by doing this: c=['r','g','b','c','m','y','k'] for i in range(30): print c[i%len(c)] I don''t know if it's faster, but: import itertools c=['r','g','b','c','m','y','k'] for i in itertools.islice(itertools.cycle(c), 30): ... print i Whenever you're using itertools, the smart money's on "yes, it's faster";-). E.g., on a slow, old iBook...: kallisti:~ alex\$ python -mtimeit -s'c="rgbcmyk"' 'for i in range(30): c[i%len(c)]' 10000 loops, best of 3: 47 usec per loop kallisti:~ alex\$ python -mtimeit -s'c="rgbcmyk"; import itertools as it' 'for i in it.islice(it.cycle(c),30): i' 10000 loops, best of 3: 26.4 usec per loop Of course, if you do add back the print statements they'll take orders of magnitude more time than the cyclic access, so /F's point on premature optimization may well be appropriate. But, if you're doing something VERY speedy with each item you access, maybe roughly halving the overhead for the cyclic access itself MIGHT be measurable (maybe not; it IS but a few microseconds, after all). I like itertools' approach because it's higher-abstraction and more direct. Its blazing speed is just a trick to sell it to conservative curmudgeons who don't see abstraction as an intrinsic good -- some of those are swayed by microseconds;-) Alex Jul 18 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.