odd behavior

Discussion in 'Python' started by Greg, Nov 11, 2005.

  1. Greg

    Greg Guest

    Forgive me, and be kind, as I am just a newby learning this language
    out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
    strange
    >>> x = ['aardvark', 'abalone', 'acme', 'add',

    'aerate']
    >>> x.sort(key=len)
    >>> x

    ['add', 'acme', 'aerate', 'abalone', 'aardvark']
    >>> x.sort(reverse=True)
    >>> x

    ['aerate', 'add', 'acme', 'abalone', 'aardvark']
    The function called on line 4, at least to me, should work on x as it
    was on line 3, not the previously existing x on line 1. What gives?
    By the way these functions do not exist in 2.3.5 so they must be newly
    implemented.
    Greg, Nov 11, 2005
    #1
    1. Advertising

  2. On 11 Nov 2005 11:34:47 -0800, Greg <> wrote:
    > Forgive me, and be kind, as I am just a newby learning this language
    > out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
    > strange
    > >>> x = ['aardvark', 'abalone', 'acme', 'add',

    > 'aerate']
    > >>> x.sort(key=len)
    > >>> x

    > ['add', 'acme', 'aerate', 'abalone', 'aardvark']
    > >>> x.sort(reverse=True)
    > >>> x

    > ['aerate', 'add', 'acme', 'abalone', 'aardvark']
    > The function called on line 4, at least to me, should work on x as it
    > was on line 3, not the previously existing x on line 1. What gives?


    The key option defaults to an alphabetic sort *every time* you call
    sort, so if you want to change this, you must call for your sort key
    each time. To do what you want, roll the sorts into one step:

    >>> x.sort(key=len, reverse=True)
    >>> x

    ['aardvark', 'abalone', 'aerate', 'acme', 'add']


    --
    Kristian

    kristian.zoerhoff(AT)gmail.com
    zoerhoff(AT)freeshell.org
    Kristian Zoerhoff, Nov 11, 2005
    #2
    1. Advertising

  3. Greg

    Lee Harr Guest

    On 2005-11-11, Kristian Zoerhoff <> wrote:
    > On 11 Nov 2005 11:34:47 -0800, Greg <> wrote:
    >> Forgive me, and be kind, as I am just a newby learning this language
    >> out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
    >> strange
    >> >>> x = ['aardvark', 'abalone', 'acme', 'add',

    >> 'aerate']
    >> >>> x.sort(key=len)
    >> >>> x

    >> ['add', 'acme', 'aerate', 'abalone', 'aardvark']
    >> >>> x.sort(reverse=True)
    >> >>> x

    >> ['aerate', 'add', 'acme', 'abalone', 'aardvark']
    >> The function called on line 4, at least to me, should work on x as it
    >> was on line 3, not the previously existing x on line 1. What gives?

    >
    > The key option defaults to an alphabetic sort *every time* you call
    > sort, so if you want to change this, you must call for your sort key
    > each time. To do what you want, roll the sorts into one step:
    >
    >>>> x.sort(key=len, reverse=True)
    >>>> x

    > ['aardvark', 'abalone', 'aerate', 'acme', 'add']
    >
    >



    .... or just reverse it after:

    >>> x.sort(key=len)
    >>> x.reverse()
    >>> x

    ['aardvark', 'abalone', 'aerate', 'acme', 'add']
    Lee Harr, Nov 11, 2005
    #3
  4. On Fri, 11 Nov 2005 11:34:47 -0800, Greg wrote:

    > Forgive me, and be kind, as I am just a newby learning this language
    > out of M.L. Hetland's book. The following behavior of 2.4.1 seems very
    > strange


    >>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']
    >>>> x.sort(key=len)
    >>>> x

    > ['add', 'acme', 'aerate', 'abalone', 'aardvark']
    >>>> x.sort(reverse=True)
    >>>> x

    > ['aerate', 'add', 'acme', 'abalone', 'aardvark']


    > The function called on line 4, at least to me, should work on x as it
    > was on line 3, not the previously existing x on line 1. What gives?


    Why do you think it isn't operating on x as it is? The second sort is
    sorting in reverse lexicographic order, which is the result you get.

    I'm not running Python 2.4 so I can't test this, but to get the result you
    want I guess you want this:

    py> x.sort(key=len, reverse=True)
    py> x
    ['aardvark', 'abalone', 'aerate', 'acme', 'add']

    or:

    py> x.sort(key=len)
    py> x.reverse()


    --
    Steven.
    Steven D'Aprano, Nov 12, 2005
    #4
    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. Elliot M. Rodriguez

    PLEASE HELP = odd TextChanged behavior

    Elliot M. Rodriguez, Oct 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    314
    Elliot M. Rodriguez
    Oct 22, 2003
  2. Guest

    Step-thru code - odd behavior

    Guest, May 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    375
    Guest
    Jun 1, 2004
  3. =?Utf-8?B?Q2hyaXM=?=
    Replies:
    1
    Views:
    315
    Karl Seguin
    Jan 7, 2005
  4. news.microsoft.com

    EXTREMELY odd cookie behavior

    news.microsoft.com, Mar 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    304
    Joerg Jooss
    Mar 15, 2005
  5. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,062
    Richard Heathfield
    Feb 18, 2007
Loading...

Share This Page