Re: for loop question

Discussion in 'Python' started by Tim Chase, Oct 10, 2007.

  1. Tim Chase

    Tim Chase Guest

    > 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
    ....
    H e
    e l
    l l
    l o
    o
    w
    w o
    o r
    r l
    l d
    >>>


    -tkc
     
    Tim Chase, Oct 10, 2007
    #1
    1. Advertising

  2. Tim Chase

    Paul Hankin Guest

    On Oct 10, 9:12 pm, Tim Chase <> 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
     
    Paul Hankin, Oct 10, 2007
    #2
    1. Advertising

  3. Tim Chase

    Tim Chase Guest

    Paul Hankin wrote:
    > On Oct 10, 9:12 pm, Tim Chase <> wrote:
    >> >>> 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


    Very nice!

    I second this solution as better than my original. The only
    "improvement" (in quotes, because it might be more work/opacity
    than the problem merits) might be to use izip/islice from
    itertools to do the evaluation lazily if "test" gets large:

    from itertools import izip, islice
    for a,b in izip(test, islice(test, 1, None)):
    print a,b

    [side note/question]
    What's with islice having the first optional paramenter expand as
    the stop/third argument by default:

    islice(test, 1) -> stop at 1
    islice(test, 1, 2) -> start at 1, stop at 2

    islice (in python2.4) doesn't even take kword params, so you
    can't force it like

    islice(test, start=1)

    but instead must specify a stop parameter, even if it's None:

    islice(test, 1, None)

    Seems bogus, IMHO.

    -tkc
     
    Tim Chase, Oct 10, 2007
    #3
  4. On Oct 10, 4:12 pm, Tim Chase <> 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
    > ...
    > H e
    > e l
    > l l
    > l o
    > o
    > w
    > w o
    > o r
    > r l
    > l d
    > >>>

    >
    > -tkc


    Or generalized for arbitrary iterables, number of items at a time,
    combination function and stopping criterion:

    from itertools import islice, takewhile, repeat

    def taking(iterable, n, combine=tuple, pred=bool):
    iterable = iter(iterable)
    return takewhile(pred, (combine(islice(iterable,n)) for _ in
    repeat(0)))

    >>> for p in taking(test,2): print p


    (u'H', u'e')
    (u'l', u'l')
    (u'o', u' ')
    (u'W', u'o')
    (u'r', u'l')
    (u'd',)

    >>> for p in taking(test,2, combine=''.join): print p


    He
    ll
    o
    Wo
    rl
    d


    George
     
    George Sakkis, Oct 10, 2007
    #4
  5. Tim Chase

    Larry Bates Guest

    Tim Chase 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
    > ...
    > H e
    > e l
    > l l
    > l o
    > o
    > w
    > w o
    > o r
    > r l
    > l d
    > >>>

    >
    > -tkc
    >
    >
    >

    import itertools

    test = u"Hello World"
    ltest=["'%s'" % c for c in test]

    for a, b in itertools.izip(ltest, ltest[1:]):
    print x, y

    'H' 'e'
    'e' 'l'
    'l' 'l'
    'l' 'o'
    'o' ' '
    ' ' 'W'
    'W' 'o'
    'o' 'r'
    'r' 'l'
    'l' 'd'

    -Larry
     
    Larry Bates, Oct 10, 2007
    #5
  6. On Wed, 10 Oct 2007 20:25:00 +0000, Paul Hankin wrote:

    >> 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


    May be unfortunately slow if test is half a gigabyte of data, what with
    essentially making three copies of it. Lazy procedures like the generator
    expression above are often much better.


    Just for completion, here's another way:

    prev = test[0]
    for i in xrange(1, len(test)):
    print prev, test
    prev = test



    --
    Steven.
     
    Steven D'Aprano, Oct 11, 2007
    #6
  7. Tim Chase

    Paul Hankin Guest

    On Oct 11, 4:40 am, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Wed, 10 Oct 2007 20:25:00 +0000, Paul Hankin wrote:
    > >> 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

    >
    > May be unfortunately slow if test is half a gigabyte of data, what with
    > essentially making three copies of it. Lazy procedures like the generator
    > expression above are often much better.
    >
    > Just for completion, here's another way:
    >
    > prev = test[0]
    > for i in xrange(1, len(test)):
    > print prev, test
    > prev = test


    Why not just:

    for i in xrange(len(test) - 1):
    print test, test[i + 1]

    --
    Paul Hankin
     
    Paul Hankin, Oct 11, 2007
    #7
  8. Tim Chase

    Paul Rubin Guest

    Steven D'Aprano <> writes:
    > >> >>> 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

    >
    > May be unfortunately slow if test is half a gigabyte of data, what with
    > essentially making three copies of it. Lazy procedures like the generator
    > expression above are often much better.


    Untested:

    from itertools import izip, islice
    for a,b in izip(test, islice(test,1,None)):
    print a,b

    seems more natural.
     
    Paul Rubin, Oct 11, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,386
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,241
    Tim Slattery
    Mar 30, 2005
  3. takayuki
    Replies:
    2
    Views:
    281
    Calvin Spealman
    Jun 16, 2008
  4. takayuki
    Replies:
    17
    Views:
    473
    John Salerno
    Jun 17, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    388
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page