Nested lists or conditional logic?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mike beck

    Nested lists or conditional logic?

    this is not all complex, but as a noob i'm having a hard time getting
    my head around it.

    i have a list of items. i need to print the items in batches of x,
    with a summary line after each complete or partial batch, and a total
    line at the end of the job.

    if the list looks like ['line1','line2' ,'line3'], and my ITEMSINBATCH
    = 1, the output should look like this:

    line1
    ---summary line---
    line2
    ---summary line---
    line3
    ---summary line---
    ---total line---

    if ITEMSINBATCH = 2, the output should look like this:

    line1
    line2
    ---summary line---
    line3
    ---summary line---
    ---total line---

    i've done the following, which works, but it seems like there must be
    a better/simpler/faster way to do this with nested loops. ideas?


    # ---------------------Begin code---------------------
    ITEMSINBATCH = 1;
    ARBITRARYNUM = 51;

    # create a list to work with
    myList = ['1']*ARBITRARYNUM;

    for i in range( len(myList) ):
    # avoid starting at 0
    count = i + 1;

    print "The item is:",myList[i],'\t',count;

    # ensure that the batch summary line is printed every ITEMSINBATCH
    # times but not if the number of items is evenly divisible by
    # ITEMSINBATCH, in which case both the inner print and the outer
    # print would execute and we'd get consecutive batch summary lines
    if ( (count) % ITEMSINBATCH ) is 0 and count != len(myList)::
    print "-----Add batch summary line-----";

    # add a final batch line for those trailing items
    print "------Add batch summary line------And BTW, i is", count;

    # and add the final summary line
    print "------Add file summary------";

    # ---------------------End code---------------------

    TIA,
    mike
  • Joe Francia

    #2
    Re: Nested lists or conditional logic?

    mike beck wrote:[color=blue]
    > this is not all complex, but as a noob i'm having a hard time getting
    > my head around it.
    >
    > i have a list of items. i need to print the items in batches of x,
    > with a summary line after each complete or partial batch, and a total
    > line at the end of the job.
    >
    > if the list looks like ['line1','line2' ,'line3'], and my ITEMSINBATCH
    > = 1, the output should look like this:
    >
    > line1
    > ---summary line---
    > line2
    > ---summary line---
    > line3
    > ---summary line---
    > ---total line---
    >
    > if ITEMSINBATCH = 2, the output should look like this:
    >
    > line1
    > line2
    > ---summary line---
    > line3
    > ---summary line---
    > ---total line---
    >
    > i've done the following, which works, but it seems like there must be
    > a better/simpler/faster way to do this with nested loops. ideas?
    >
    >
    > # ---------------------Begin code---------------------
    > ITEMSINBATCH = 1;
    > ARBITRARYNUM = 51;
    >
    > # create a list to work with
    > myList = ['1']*ARBITRARYNUM;
    >
    > for i in range( len(myList) ):
    > # avoid starting at 0
    > count = i + 1;
    >
    > print "The item is:",myList[i],'\t',count;
    >
    > # ensure that the batch summary line is printed every ITEMSINBATCH
    > # times but not if the number of items is evenly divisible by
    > # ITEMSINBATCH, in which case both the inner print and the outer
    > # print would execute and we'd get consecutive batch summary lines
    > if ( (count) % ITEMSINBATCH ) is 0 and count != len(myList)::
    > print "-----Add batch summary line-----";
    >
    > # add a final batch line for those trailing items
    > print "------Add batch summary line------And BTW, i is", count;
    >
    > # and add the final summary line
    > print "------Add file summary------";
    >
    > # ---------------------End code---------------------[/color]


    I'm not sure this is faster, but it sure is simpler:

    mylist = range(23)
    batchsize = 4

    for i in range(0,len(myl ist),batchsize) :
    for l in mylist[i:i+batchsize]:
    print l
    print "---summary line---"
    print "---file summary---"



    --
    Soraia: http://www.soraia.com

    Comment

    • Peter Otten

      #3
      Re: Nested lists or conditional logic?

      mike beck wrote:
      [color=blue]
      > this is not all complex, but as a noob i'm having a hard time getting
      > my head around it.
      >
      > i have a list of items. i need to print the items in batches of x,
      > with a summary line after each complete or partial batch, and a total
      > line at the end of the job.[/color]

      [...]
      [color=blue]
      > i've done the following, which works, but it seems like there must be
      > a better/simpler/faster way to do this with nested loops. ideas?[/color]

      Your code looks OK to me (apart from the duplicate colon). Here's a "no
      maths" variant that uses nested loops:

      from itertools import islice

      ITEMSINBATCH = 3;
      ARBITRARYNUM = 11;

      sample = ["item #%d" % i for i in range(ARBITRARY NUM)]
      it = iter(sample)

      while True:
      more = False
      for item in islice(it, ITEMSINBATCH):
      more = True
      print item
      if not more: break
      print "summary"

      I'm generally fond of the itertools module; this time I didn't find a way to
      get rid of the ugly more flag, though. Implementing your own iterator with
      a hasMore() method seems overkill.

      Peter

      Comment

      • Terry Reedy

        #4
        Re: Nested lists or conditional logic?


        "mike beck" <cmichaelbeck@h otmail.com> wrote in message
        news:ead93a6.04 01280956.41fd41 93@posting.goog le.com...[color=blue]
        > this is not all complex, but as a noob i'm having a hard time getting
        > my head around it.
        >
        > i have a list of items. i need to print the items in batches of x,
        > with a summary line after each complete or partial batch, and a total
        > line at the end of the job.[/color]
        [color=blue]
        > i've done the following, which works, but it seems like there must be
        > a better/simpler/faster way to do this with nested loops. ideas?[/color]

        The awkwardness is inherent in having non-0 remainders. An explicit
        counter strikes me as both simple and fast. Maybe because what you did is
        exactly the way I have done similar things. But I would probably make
        print_summary a function to avoid duplicating code in two places.

        Terry J. Reedy




        Comment

        • Peter Otten

          #5
          Re: Nested lists or conditional logic?

          Here's another odd one:

          from itertools import islice

          ITEMSINBATCH = 3;
          ARBITRARYNUM = 11;

          sample = ["item #%d" % i for i in range(ARBITRARY NUM)]

          def batches(s, n):
          it = iter(s)
          n -= 1
          def batch(next):
          yield next
          for item in islice(it, n):
          yield item
          while True:
          yield batch(it.next() )

          for batch in batches(sample, ITEMSINBATCH):
          for item in batch:
          print item
          print "summary"

          The client code is as clean as you can get...

          Peter

          Comment

          Working...