[Tutor] Multiple Simultaneous Loops
Ed Singleton
singletoned at gmail.com
Thu Sep 15 12:34:46 CEST 2005
Wonderful, thank you all of you.
zip, enumerate, and count seem to do everything I want, though I do think
for f, x in bunch_of_files, range(z):
is a little more intuitive than
for f, x in zip(bunch_of_files, range(z)):
Thanks
Ed
On 15/09/05, Kent Johnson <kent37 at tds.net> wrote:
> Ed Singleton wrote:
> > I roughly want to be able to do:
> >
> > for f, x in bunch_of_files, range(z):
> >
> > so that x iterates through my files, and y iterates through something else.
> >
> > Is this something I can do?
>
> In the general case use zip():
> for f, x in zip(bunch_of_files, range(z)):
>
> In this case, where the second item is just the index to the loop, use enumerate() instead of range() and zip()
> for x, f in enumerate(bunch_of_files):
>
> > If so, what would be the best way to create a range of indeterminate length?
>
> itertools.count() generates an "unlimited" sequence.
>
> > If not, is there a nice way I can do it, rather than than incrementing
> > a variable (x = x + 1) every loop?
> >
> > Or maybe can I access the number of times the loop has run? ('x = x +
> > 1' is so common there must be some more attractive shortcut).
>
> enumerate()
>
> > So far in learning Python I've founbd that when I feel you should be
> > able to do something, then you can.
>
> Yep :-)
>
> Kent
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list