for loop question

Rafael Sachetto rsachetto at gmail.com
Wed Oct 10 16:29:09 EDT 2007


Very nice solution :)

On 10/10/07, Paul Hankin <paul.hankin at gmail.com> wrote:
>
> On Oct 10, 9:12 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> > > test = u"Hello World"
> >
> > > for cur,next in test:
> > >     print cur,next
> >
> > > Ideally, this would output:
> >
> > > 'H', 'e'
> > > 'e', 'l'
> > > 'l', 'l'
> > > 'l', 'o'
> > > etc...
> >
> > > Of course, the for loop above isn't valid at all. I am just giving an
> > > example of what I'm trying to accomplish. Anyone know how I can
> achieve the
> > > goal in the example above? Thanks.
> >
> > A "works-for-me":
> >
> >  >>> pairs = (test[i:i+2] for i in xrange(len(test)-1))
> >  >>> for a,b in pairs:
> > ...     print a,b
>
> for a, b in zip(test, test[1:]):
>   print a, b
>
> --
> Paul Hankin
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Rafael Sachetto Oliveira

Sir - Simple Image Resizer
http://rsachetto.googlepages.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071010/a0187220/attachment.html>


More information about the Python-list mailing list