Python program to sort lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    Python program to sort lists

    When I run the code in only returns the new list with only the min of the list it doesn't go throughtout the list to return other numbers in the list.
    [CODE=python]
    >>>def short(list2):
    while len(list2) != 0:
    new = []
    loop = -1
    a = min(list2)
    for s in list2:
    loop += 1
    if s == a:
    new.append(a)
    list.pop(loop)
    return list2[/CODE]
    Last edited by bartonc; Sep 16 '07, 02:44 PM. Reason: Added [CODE=python][/CODE] tags.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by thatos
    When I run the code in only returns the new list with only the min of the list it doesn't go throughtout the list to return other numbers in the list.

    >>>def short(list2):
    while len(list2) != 0:
    new = []
    loop = -1
    a = min(list2)
    for s in list2:
    loop += 1
    if s == a:
    new.append(a)
    list.pop(loop)
    return list2
    Why are you doing this? There is a built in sorted() function that sorts an itarable and lists have a list.sort() function which sorts in place.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by thatos
      When I run the code in only returns the new list with only the min of the list it doesn't go throughtout the list to return other numbers in the list.
      [CODE=python]
      >>>def short(list2):
      while len(list2) != 0:
      new = []
      loop = -1
      a = min(list2)
      for s in list2:
      loop += 1
      if s == a:
      new.append(a)
      list.pop(loop)
      return list2[/CODE]
      The problem here is (and I believe that writing sort routines is very good practice) that you hit the return inside the inner loop. You don't want to return until the whole sort is complete: Also, you are continually making a new, empty list inside the while loop.
      [CODE=python]
      def short(list2):
      newList = [] # do this outside to loop
      while len(list2) != 0:
      loop = -1
      a = min(list2)
      for s in list2:
      loop += 1
      if s == a:
      newList.append( a)
      list2.pop(loop)
      return newList[/CODE]Note, however, that a list sent into this routine will end up empty due to the "pass-by-reference" nature of Python and most other programming languages.

      Comment

      • thatos
        New Member
        • Aug 2007
        • 105

        #4
        Originally posted by bartonc
        The problem here is (and I believe that writing sort routines is very good practice) that you hit the return inside the inner loop. You don't want to return until the whole sort is complete: Also, you are continually making a new, empty list inside the while loop.
        [CODE=python]
        def short(list2):
        newList = [] # do this outside to loop
        while len(list2) != 0:
        loop = -1
        a = min(list2)
        for s in list2:
        loop += 1
        if s == a:
        newList.append( a)
        list2.pop(loop)
        return newList[/CODE]Note, however, that a list sent into this routine will end up empty due to the "pass-by-reference" nature of Python and most other programming languages.
        when I want to sort [3,2,1] it return [1,1,3]

        Comment

        • thatos
          New Member
          • Aug 2007
          • 105

          #5
          Originally posted by bartonc
          The problem here is (and I believe that writing sort routines is very good practice) that you hit the return inside the inner loop. You don't want to return until the whole sort is complete: Also, you are continually making a new, empty list inside the while loop.
          [CODE=python]
          def short(list2):
          newList = [] # do this outside to loop
          while len(list2) != 0:
          loop = -1
          a = min(list2)
          for s in list2:
          loop += 1
          if s == a:
          newList.append( a)
          list2.pop(loop)
          return newList[/CODE]Note, however, that a list sent into this routine will end up empty due to the "pass-by-reference" nature of Python and most other programming languages.
          thanks for your help you helped me a lot and at least I'm improving

          Comment

          Working...