Confused about a list.sort()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amy G

    Confused about a list.sort()

    I have a list of numbers... actully I have two lists, List 1 is a list of
    number strings and List2 is one of numbers.

    List 1 example:
    List1 = [ '20040124123000 ', '20040124123001 ', '20040125012456 ']

    List 2 example:
    List2 = [ 20040124123000L , 20040124123001L , '20040125012456 L]

    When I try either:
    List1 = List1.sort ... or
    List2 = List2.sirt

    and then...
    print List1... or
    print List2

    I get None.

    Why is this?
    How do I remedy this problem?


  • Terry Reedy

    #2
    Re: Confused about a list.sort()


    "Amy G" <amy-g-art@cox.net> wrote in message
    news:S6FRb.2098 $P17.1139@fed1r ead03...[color=blue]
    > I have a list of numbers... actully I have two lists, List 1 is a list of
    > number strings and List2 is one of numbers.
    >
    > List 1 example:
    > List1 = [ '20040124123000 ', '20040124123001 ', '20040125012456 ']
    >
    > List 2 example:
    > List2 = [ 20040124123000L , 20040124123001L , '20040125012456 L]
    >
    > When I try either:
    > List1 = List1.sort ... or
    > List2 = List2.sirt
    >
    > and then...
    > print List1... or
    > print List2
    >
    > I get None.
    >
    > Why is this?
    > How do I remedy this problem?[/color]

    Read the library reference manual on builtin objects - sequences - lists -
    methods.

    Seriously.

    TJR




    Comment

    • David Goodger

      #3
      Re: Confused about a list.sort()

      list.sort() sorts the list in-place; it doesn't return a new list.
      Instead of "List1 = List1.sort()" just do "List1.sort ()".

      See the FAQ entry:


      -- David Goodger


      Comment

      • Terry Carroll

        #4
        Re: Confused about a list.sort()

        On Tue, 27 Jan 2004 18:28:09 -0800, "Amy G" <amy-g-art@cox.net> wrote:
        [color=blue]
        >I have a list of numbers... actully I have two lists, List 1 is a list of
        >number strings and List2 is one of numbers.
        >
        >List 1 example:
        >List1 = [ '20040124123000 ', '20040124123001 ', '20040125012456 ']
        >
        >List 2 example:
        >List2 = [ 20040124123000L , 20040124123001L , '20040125012456 L]
        >
        >When I try either:
        >List1 = List1.sort ... or
        >List2 = List2.sirt
        >
        >and then...
        >print List1... or
        >print List2
        >
        >I get None.
        >
        >Why is this?[/color]

        Yeah, most everyone who uses sort() for the first time gets bit by this.

        Sort() sorts the list in place, and returns None:
        [color=blue][color=green][color=darkred]
        >>> list1=[20, 40, 60, 80, 10, 30, 50]
        >>> list1[/color][/color][/color]
        [20, 40, 60, 80, 10, 30, 50][color=blue][color=green][color=darkred]
        >>> list1.sort()
        >>> list1[/color][/color][/color]
        [10, 20, 30, 40, 50, 60, 80]

        So, to sort list1, you just use list1.sort(), not foo = list1.sort()


        Comment

        • wes weston

          #5
          Re: Confused about a list.sort()

          Amy,
          Switch from windoze to linux and provide screen dumps.

          wes@linux:~> python
          Python 2.3.3c1 (#3, Dec 26 2003, 16:36:50)
          [GCC 3.3.1 (SuSE Linux)] on linux2
          Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
          >>> list = [3,9,2]
          >>> list.sort()
          >>> list[/color][/color][/color]
          [2, 3, 9]



          Amy G wrote:[color=blue]
          > I have a list of numbers... actully I have two lists, List 1 is a list of
          > number strings and List2 is one of numbers.
          >
          > List 1 example:
          > List1 = [ '20040124123000 ', '20040124123001 ', '20040125012456 ']
          >
          > List 2 example:
          > List2 = [ 20040124123000L , 20040124123001L , '20040125012456 L]
          >
          > When I try either:
          > List1 = List1.sort ... or
          > List2 = List2.sirt
          >
          > and then...
          > print List1... or
          > print List2
          >
          > I get None.
          >
          > Why is this?
          > How do I remedy this problem?
          >
          >[/color]

          Comment

          • Dang Griffith

            #6
            Re: Confused about a list.sort()

            On Wed, 28 Jan 2004 15:22:05 GMT, wes weston <wweston@att.ne t> wrote:
            [color=blue]
            > Switch from windoze to linux and provide screen dumps.[/color]

            I don't understand part of your comment. Windows can provide screen
            dumps also. That's not a reason to switch. :-)
            --dang

            Comment

            Working...