Confused about a list.sort()

Discussion in 'Python' started by Amy G, Jan 28, 2004.

  1. Amy G

    Amy G Guest

    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, '20040125012456L]

    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?
     
    Amy G, Jan 28, 2004
    #1
    1. Advertising

  2. Amy G

    Terry Reedy Guest

    "Amy G" <> wrote in message
    news:S6FRb.2098$P17.1139@fed1read03...
    > 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, '20040125012456L]
    >
    > 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?


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

    Seriously.

    TJR
     
    Terry Reedy, Jan 28, 2004
    #2
    1. Advertising

  3. David Goodger, Jan 28, 2004
    #3
  4. On Tue, 27 Jan 2004 18:28:09 -0800, "Amy G" <> wrote:

    >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, '20040125012456L]
    >
    >When I try either:
    >List1 = List1.sort ... or
    >List2 = List2.sirt
    >
    >and then...
    >print List1... or
    >print List2
    >
    >I get None.
    >
    >Why is this?


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

    Sort() sorts the list in place, and returns None:

    >>> list1=[20, 40, 60, 80, 10, 30, 50]
    >>> list1

    [20, 40, 60, 80, 10, 30, 50]
    >>> list1.sort()
    >>> list1

    [10, 20, 30, 40, 50, 60, 80]

    So, to sort list1, you just use list1.sort(), not foo = list1.sort()
     
    Terry Carroll, Jan 28, 2004
    #4
  5. Amy G

    wes weston Guest

    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.
    >>> list = [3,9,2]
    >>> list.sort()
    >>> list

    [2, 3, 9]



    Amy G wrote:
    > 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, '20040125012456L]
    >
    > 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?
    >
    >
     
    wes weston, Jan 28, 2004
    #5
  6. On Wed, 28 Jan 2004 15:22:05 GMT, wes weston <> wrote:

    > Switch from windoze to linux and provide screen dumps.


    I don't understand part of your comment. Windows can provide screen
    dumps also. That's not a reason to switch. :)
    --dang
     
    Dang Griffith, Jan 29, 2004
    #6
  7. Amy G

    James Kew Guest

    "wes weston" <> wrote in message
    news:xsQRb.28535$...
    > Switch from windoze to linux and provide screen dumps.
    >
    > wes@linux:~> python
    > Python 2.3.3c1 (#3, Dec 26 2003, 16:36:50)


    Ah, that's nice: a simple question about list.sort()'s behaviour answered
    with a dose of Linux advocacy. Everyone else managed to answer the question
    at hand.

    FWIW, Python works very nicely at the Windows command-line too, "screen
    dumps" and all:

    C:\>python
    ActivePython 2.3.2 Build 232 (ActiveState Corp.) based on
    Python 2.3.2 (#49, Nov 13 2003, 10:34:54) [MSC v.1200 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>


    although I have to admit I usually prefer PythonWin's interactive window.

    ISTR discussion on python-dev about adding a sorted() method to list,
    returning a sorted copy of the list -- did that ever reach a conclusion?

    James
     
    James Kew, Jan 30, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank & Janny Plaza
    Replies:
    4
    Views:
    632
    Kenneth Hutson
    Sep 23, 2003
  2. Brian Blais
    Replies:
    23
    Views:
    597
    Ron Adam
    Feb 28, 2006
  3. Replies:
    11
    Views:
    743
    bruno at modulix
    May 9, 2006
  4. Replies:
    7
    Views:
    756
    Stefan Arentz
    Sep 10, 2007
  5. Navin
    Replies:
    1
    Views:
    728
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page