odd behavior

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

    #1

    odd behavior

    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[color=blue][color=green][color=darkred]
    >>> x = ['aardvark', 'abalone', 'acme', 'add',[/color][/color][/color]
    'aerate'][color=blue][color=green][color=darkred]
    >>> x.sort(key=len)
    >>> x[/color][/color][/color]
    ['add', 'acme', 'aerate', 'abalone', 'aardvark'][color=blue][color=green][color=darkred]
    >>> x.sort(reverse= True)
    >>> x[/color][/color][/color]
    ['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.

  • Kristian Zoerhoff

    #2
    Re: odd behavior

    On 11 Nov 2005 11:34:47 -0800, Greg <gstgeorge@gmai l.com> wrote:[color=blue]
    > 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[color=green][color=darkred]
    > >>> x = ['aardvark', 'abalone', 'acme', 'add',[/color][/color]
    > 'aerate'][color=green][color=darkred]
    > >>> x.sort(key=len)
    > >>> x[/color][/color]
    > ['add', 'acme', 'aerate', 'abalone', 'aardvark'][color=green][color=darkred]
    > >>> x.sort(reverse= True)
    > >>> x[/color][/color]
    > ['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?[/color]

    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:
    [color=blue][color=green][color=darkred]
    >>> x.sort(key=len, reverse=True)
    >>> x[/color][/color][/color]
    ['aardvark', 'abalone', 'aerate', 'acme', 'add']


    --
    Kristian

    kristian.zoerho ff(AT)gmail.com
    zoerhoff(AT)fre eshell.org

    Comment

    • Lee Harr

      #3
      Re: odd behavior

      On 2005-11-11, Kristian Zoerhoff <kristian.zoerh off@gmail.com> wrote:[color=blue]
      > On 11 Nov 2005 11:34:47 -0800, Greg <gstgeorge@gmai l.com> wrote:[color=green]
      >> 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[color=darkred]
      >> >>> x = ['aardvark', 'abalone', 'acme', 'add',[/color]
      >> 'aerate'][color=darkred]
      >> >>> x.sort(key=len)
      >> >>> x[/color]
      >> ['add', 'acme', 'aerate', 'abalone', 'aardvark'][color=darkred]
      >> >>> x.sort(reverse= True)
      >> >>> x[/color]
      >> ['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?[/color]
      >
      > 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:
      >[color=green][color=darkred]
      >>>> x.sort(key=len, reverse=True)
      >>>> x[/color][/color]
      > ['aardvark', 'abalone', 'aerate', 'acme', 'add']
      >
      >[/color]


      .... or just reverse it after:
      [color=blue][color=green][color=darkred]
      >>> x.sort(key=len)
      >>> x.reverse()
      >>> x[/color][/color][/color]
      ['aardvark', 'abalone', 'aerate', 'acme', 'add']

      Comment

      • Steven D'Aprano

        #4
        Re: odd behavior

        On Fri, 11 Nov 2005 11:34:47 -0800, Greg wrote:
        [color=blue]
        > 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[/color]
        [color=blue][color=green][color=darkred]
        >>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']
        >>>> x.sort(key=len)
        >>>> x[/color][/color]
        > ['add', 'acme', 'aerate', 'abalone', 'aardvark'][color=green][color=darkred]
        >>>> x.sort(reverse= True)
        >>>> x[/color][/color]
        > ['aerate', 'add', 'acme', 'abalone', 'aardvark'][/color]
        [color=blue]
        > 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?[/color]

        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.

        Comment

        Working...