Small problem with print and comma

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • benjamin.cordes@blawrc.de

    #1

    Small problem with print and comma

    Hi,

    I have a small problem with my function: printList. I use print with a
    ',' . Somehow the last digit of the last number isn't printed. I wonder
    why.

    import random

    def createRandomLis t(param):
    length = param

    a = []
    """" creating random list"""
    for i in range(0,length) :
    a.append(random .randrange(100) )
    return a

    def printList(param ):
    #doesn't work
    #2 sample outputs
    # 30 70 68 6 48 60 29 48 30 38
    #sorted list
    #6 29 30 30 38 48 48 60 68 7 <--- last character missing

    #93 8 10 28 94 4 26 41 72 6
    #sorted list
    #4 6 8 10 26 28 41 72 93 9 <-- dito


    for i in range(0,len(par am)):
    print a[i],
    #works
    #for i in range(0,len(par am)-1):
    # print a[i],
    #print a[len(param)-1]


    if __name__ == "__main__":
    length = 10
    a = createRandomLis t(length)
    printList(a)

    for j in range(1,len(a)) :
    key = a[j]
    i = j-1
    while i -1 and a[i]>key:
    a[i+1] = a[i]
    i = i-1
    a[i+1] = key

    print "\n sorted list"
    printList(a)

  • faulkner

    #2
    Re: Small problem with print and comma

    why don't you iterate over the list instead of indices?
    for elem in L: print elem,

    you don't need the 0 when you call range: range(0, n) == range(n)
    the last element of a range is n-1: range(n)[-1] == n-1
    you don't need while to iterate backwards. the third argument to range
    is step.
    range(n-1, -1, -1) == [n-1, n-2, n-3, ..., 1, 0]

    benjamin.cordes @blawrc.de wrote:
    Hi,
    >
    I have a small problem with my function: printList. I use print with a
    ',' . Somehow the last digit of the last number isn't printed. I wonder
    why.
    >
    import random
    >
    def createRandomLis t(param):
    length = param
    >
    a = []
    """" creating random list"""
    for i in range(0,length) :
    a.append(random .randrange(100) )
    return a
    >
    def printList(param ):
    #doesn't work
    #2 sample outputs
    # 30 70 68 6 48 60 29 48 30 38
    #sorted list
    #6 29 30 30 38 48 48 60 68 7 <--- last character missing
    >
    #93 8 10 28 94 4 26 41 72 6
    #sorted list
    #4 6 8 10 26 28 41 72 93 9 <-- dito
    >
    >
    for i in range(0,len(par am)):
    print a[i],
    #works
    #for i in range(0,len(par am)-1):
    # print a[i],
    #print a[len(param)-1]
    >
    >
    if __name__ == "__main__":
    length = 10
    a = createRandomLis t(length)
    printList(a)
    >
    for j in range(1,len(a)) :
    key = a[j]
    i = j-1
    while i -1 and a[i]>key:
    a[i+1] = a[i]
    i = i-1
    a[i+1] = key
    >
    print "\n sorted list"
    printList(a)

    Comment

    • Tim Chase

      #3
      Re: Small problem with print and comma

      I have a small problem with my function: printList. I use print with a
      ',' . Somehow the last digit of the last number isn't printed. I wonder
      why.
      Posting actual code might help...the code you sent has a horrible
      mix of tabs and spaces. You've also got some craziness in your
      "creating random list" string. First off, it looks like you're
      using a docstring, but they only go immediately after the def
      line. I'd recommend putting it where it belongs, or changing the
      line to a comment.

      There are some unpythonic bits in here:

      printList() would usually just idiomatically be written as

      print ' '.join(a)

      although there are some int-to-string problems with that, so it
      would be written as something like

      print ' '.join([str(x) for x in listOfNumbers])

      which is efficient, and avoids the possibility of off-by-one
      errors when range(0,length) .

      Another idiom would be the list-building of createRandomLis t:

      return [random.randrang e(100) for x in xrange(0,length )]

      Additionally, this can be reduced as range/xrange assume 0 as the
      default starting point

      return [random.randrang e(100) for x in xrange(length)]

      (using xrange also avoids building an unneeded list, just to
      throw it away)

      Additionally, rather than rolling your own bubble-sort, you can
      just make use of a list's sort() method:

      a.sort()

      Other items include sensibly naming your parameters rather than
      generically calling them "param", just to reassign them to
      another name inside.

      Taking my suggestions into consideration, your original program
      condenses to

      ############### ############### ##########

      import random

      def createRandomLis t(length):
      return [random.randrang e(100) for x in xrange(length)]

      def printList(listO fNumbers):
      print ' '.join([str(x) for x in listOfNumbers])

      if __name__ == "__main__":
      length = 10
      a = createRandomLis t(length)
      printList(a)
      a.sort()
      print "sorted list"
      printList(a)

      ############### ############### ##########

      one might even change createRandomLis t to allow a little more
      flexibility:

      def createRandomLis t(length, maximum=100):
      return [random.randrang e(maximum) for x in xrange(length)]


      So it can be called as you already do, or you can specify the
      maximum as well with

      createRandomLis t(10, 42)

      for future use when 100 doesn't cut it for you in all cases.

      Just a few thoughts.

      -tkc




      Comment

      • Dustan

        #4
        Re: Small problem with print and comma

        Dennis Lee Bieber wrote:
        for i in range(0,len(par am)):
        print a[i],
        >
        for it in param:
        print it,
        That's one way. However, if you need the position (this is for future
        reference; you don't need the position number here):

        for i in range(len(param )+1):
        print a[i],

        The last position was excluded because you forgot the '+1' part,
        creating an off-by-one bug.

        Comment

        • Diez B. Roggisch

          #5
          Re: Small problem with print and comma

          Dustan wrote:
          Dennis Lee Bieber wrote:
          for i in range(0,len(par am)):
          print a[i],
          >>
          >for it in param:
          >print it,
          >
          That's one way. However, if you need the position (this is for future
          reference; you don't need the position number here):
          >
          for i in range(len(param )+1):
          print a[i],
          >
          The last position was excluded because you forgot the '+1' part,
          creating an off-by-one bug.
          No, your code creates that bug.

          However, the above is not very pythonic - if param is a iterator and not a
          sequence-protocol-adherent object, it fails. The usual way to do it is


          for i, a in enumerate(param ):
          print a,


          Diez

          Comment

          Working...