Printing with interspersed element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulo J. Matos

    Printing with interspersed element

    Hi all,

    I guess this is a recurring issue for someone who doesn't really know
    the python lib inside out. There must be a simple way to do this.
    I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
    print method for them print_obj(). Now I want to print them
    intersepersed by an element.
    If I print [x1, x2, x3] interspersed by the element 10:
    x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()

    Now, the question is, what's the best way to do this?

    I guess I could do this recursively.
    def print(el, lst):
    if len(lst) == 0:
    return
    elif len(lst) == 1:
    lst[0].print_obj()
    else:
    lst[0].print_obj()
    print el,
    print(el, lst[1:])

    Now, some considerations. This seems cumbersome (it may have errors
    has I have not tested and was written directly to the mail, but the
    idea is clear). From what I know lst[1:] creates a copy of lst without
    the first element which is really not good memory-wise.
    So, what would be the python way to do it?

    Cheers,

    --
    Paulo Jorge Matos - pocmatos at gmail.com
    Webpage: http://www.personal.soton.ac.uk/pocm
  • Grant Edwards

    #2
    Re: Printing with interspersed element

    On 2008-10-30, Paulo J. Matos <pocm@ecs.soton .ac.ukwrote:
    Hi all,
    >
    I guess this is a recurring issue for someone who doesn't really know
    the python lib inside out. There must be a simple way to do this.
    I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
    print method for them print_obj(). Now I want to print them
    intersepersed by an element.
    If I print [x1, x2, x3] interspersed by the element 10:
    x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()
    >>','.join([str(i) for i in [1,2,3,4]])
    '1,2,3,4'
    >>','.join([i.__repr__() for i in [1,2,3,4]])
    '1,2,3,4'
    >>','.join([str(i) for i in [1]])
    '1'

    --
    Grant Edwards grante Yow! What I want to find
    at out is -- do parrots know
    visi.com much about Astro-Turf?

    Comment

    • Arnaud Delobelle

      #3
      Re: Printing with interspersed element

      On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton .ac.ukwrote:
      Hi all,
      >
      I guess this is a recurring issue for someone who doesn't really know
      the python lib inside out. There must be a simple way to do this.
      I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
      print method for them print_obj(). Now I want to print them
      intersepersed by an element.
      If I print [x1, x2, x3] interspersed by the element 10:
      x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()
      >
      Now, the question is, what's the best way to do this?
      Defining a print_obj() method is probably a bad idea. What if you
      want to print to a file for example? Instead you can define a
      __str__() method for your objects and then use the join() method of
      strings like this:

      print ' 10 '.join(str(x) for x in lst)

      HTH

      --
      Arnaud

      Comment

      • Paulo J. Matos

        #4
        Re: Printing with interspersed element

        On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle
        <arnodel@google mail.comwrote:
        On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton .ac.ukwrote:
        >Hi all,
        >>
        >I guess this is a recurring issue for someone who doesn't really know
        >the python lib inside out. There must be a simple way to do this.
        >I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
        >print method for them print_obj(). Now I want to print them
        >interseperse d by an element.
        >If I print [x1, x2, x3] interspersed by the element 10:
        >x1.print_obj () 10 x2.print_obj() 10 x3.print_obj()
        >>
        >Now, the question is, what's the best way to do this?
        >
        Defining a print_obj() method is probably a bad idea. What if you
        want to print to a file for example? Instead you can define a
        __str__() method for your objects and then use the join() method of
        strings like this:
        >
        print ' 10 '.join(str(x) for x in lst)
        >
        Thanks for the tip but that has an issue when dealing with potentially
        millions of objects. You are creating a string in memory to then dump
        to a file [or screen] while you could dump to the file [or screen] as
        you go through the original string. Right?


        --
        Paulo Jorge Matos - pocmatos at gmail.com
        Webpage: http://www.personal.soton.ac.uk/pocm

        Comment

        • Arnaud Delobelle

          #5
          Re: Printing with interspersed element


          On 30 Oct 2008, at 21:10, Paulo J. Matos wrote:
          On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle
          <arnodel@google mail.comwrote:
          >On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton .ac.ukwrote:
          >>Hi all,
          >>>
          >>I guess this is a recurring issue for someone who doesn't really
          >>know
          >>the python lib inside out. There must be a simple way to do this.
          >>I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
          >>print method for them print_obj(). Now I want to print them
          >>intersepers ed by an element.
          >>If I print [x1, x2, x3] interspersed by the element 10:
          >>x1.print_obj( ) 10 x2.print_obj() 10 x3.print_obj()
          >>>
          >>Now, the question is, what's the best way to do this?
          >>
          >Defining a print_obj() method is probably a bad idea. What if you
          >want to print to a file for example? Instead you can define a
          >__str__() method for your objects and then use the join() method of
          >strings like this:
          >>
          >print ' 10 '.join(str(x) for x in lst)
          >>
          >
          Thanks for the tip but that has an issue when dealing with potentially
          millions of objects. You are creating a string in memory to then dump
          to a file [or screen] while you could dump to the file [or screen] as
          you go through the original string. Right?
          Why would you want to print millions of objects on the screen?

          As for writing to a file, a million objects will probably mean a few
          tens of million bytes which is not that much. Your proposed method
          would not work as the python call stack would explode first. Here is
          one that may meet your approval (it still requires a __str__ method on
          your objects but you can adapt it easily):

          def print_with_sep( sep, iterable, file=sys.stdout ):
          iterator = iter(iterable)
          try:
          file.write(str( iterator.next() ))
          for item in iterator:
          file.write(sep)
          file.write(str( item))
          except StopIteration:
          pass

          # Use like this:
          >>print_with_se p(' 10 ', [obj1, obj2, obj3])
          --
          Arnaud

          Comment

          • Grant Edwards

            #6
            Re: Printing with interspersed element

            On 2008-10-30, Paulo J. Matos <pocmatos@gmail .comwrote:
            On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle
            ><arnodel@googl email.comwrote:
            >On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton .ac.ukwrote:
            >>Hi all,
            >>>
            >>I guess this is a recurring issue for someone who doesn't really know
            >>the python lib inside out. There must be a simple way to do this.
            >>I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
            >>print method for them print_obj(). Now I want to print them
            >>intersepers ed by an element.
            >>If I print [x1, x2, x3] interspersed by the element 10:
            >>x1.print_obj( ) 10 x2.print_obj() 10 x3.print_obj()
            >>>
            >>Now, the question is, what's the best way to do this?
            >>
            >Defining a print_obj() method is probably a bad idea. What if you
            >want to print to a file for example? Instead you can define a
            >__str__() method for your objects and then use the join() method of
            >strings like this:
            >>
            >print ' 10 '.join(str(x) for x in lst)
            >
            Thanks for the tip but that has an issue when dealing with potentially
            millions of objects. You are creating a string in memory to then dump
            to a file [or screen] while you could dump to the file [or screen] as
            you go through the original string. Right?
            If you want to do it "on the fly", then try something like this:

            iter = [1,2,3,4,5].__iter__()
            sys.stdout.writ e(str(iter.next ()))
            for n in iter:
            sys.stdout.writ e(',' +str(n))

            --
            Grant Edwards grante Yow! The SAME WAVE keeps
            at coming in and COLLAPSING
            visi.com like a rayon MUU-MUU ...

            Comment

            • Matimus

              #7
              Re: Printing with interspersed element

              On Oct 30, 2:10 pm, "Paulo J. Matos" <pocma...@gmail .comwrote:
              On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle
              >
              >
              >
              <arno...@google mail.comwrote:
              On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton .ac.ukwrote:
              Hi all,
              >
              I guess this is a recurring issue for someone who doesn't really know
              the python lib inside out. There must be a simple way to do this.
              I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
              print method for them print_obj(). Now I want to print them
              intersepersed by an element.
              If I print [x1, x2, x3] interspersed by the element 10:
              x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()
              >
              Now, the question is, what's the best way to do this?
              >
              Defining a print_obj() method is probably a bad idea.  What if you
              want to print to a file for example?  Instead you can define a
              __str__() method for your objects and then use the join() method of
              strings like this:
              >
              print ' 10 '.join(str(x) for x in lst)
              >
              Thanks for the tip but that has an issue when dealing with potentially
              millions of objects. You are creating a string in memory to then dump
              to a file [or screen] while you could dump to the file [or screen] as
              you go through the original string. Right?
              Then I hope you are using stackless, because you are going to stack
              overflow _way_ before you recurse 1 million times.

              def print_list(seq, sep=','):
              seq = iter(seq)
              print seq.next(),
              for item in seq:
              print sep,
              print item,
              print

              Matt

              Comment

              • Marc 'BlackJack' Rintsch

                #8
                Re: Printing with interspersed element

                On Thu, 30 Oct 2008 16:40:17 -0500, Grant Edwards wrote:
                If you want to do it "on the fly", then try something like this:
                >
                iter = [1,2,3,4,5].__iter__()
                sys.stdout.writ e(str(iter.next ()))
                for n in iter:
                sys.stdout.writ e(',' +str(n))
                Maybe without shadowing the built in `iter()` and without calling the
                "magic method" directly:

                iterator = iter([1, 2, 3, 4, 5])

                Ciao,
                Marc 'BlackJack' Rintsch

                Comment

                • Kirk Strauser

                  #9
                  Re: Printing with interspersed element

                  At 2008-10-30T21:10:09Z, "Paulo J. Matos" <pocmatos@gmail .comwrites:
                  Thanks for the tip but that has an issue when dealing with potentially
                  millions of objects. You are creating a string in memory to then dump
                  to a file [or screen] while you could dump to the file [or screen] as
                  you go through the original string. Right?
                  How about:

                  def alternator(lst, sep):
                  for index, item in enumerate(lst):
                  if index:
                  yield sep
                  yield item

                  for item in alternator(list _of_objects, 10):
                  print item,

                  --
                  Kirk Strauser
                  The Day Companies

                  Comment

                  Working...