[Tutor] creating a tab delimited filename

Kent Johnson kent37 at tds.net
Thu Mar 17 12:05:41 CET 2005


C Smith wrote:
>  Here's an example that
> cycles through 3 number, 0, 1, and 2:
> 
> ###
>  >>> j=0
>  >>> for i in range(10):
> ..   print j
> ..   j = (j+1)%3
> ..
> 0
> 1
> 2
> 0
> 1
> 2
> 0
> 1
> 2
> 0
> ###

A nice way to do this is with itertools.cycle():
  >>> import itertools
  >>> cyc = itertools.cycle(range(3))
  >>> for i in range(10):
  ...   print cyc.next()
  ...
0
1
2
0
1
2
0
1
2
0

Kent



More information about the Tutor mailing list