best Pythonic way to do this sort: Python newb

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

    #1

    best Pythonic way to do this sort: Python newb

    Hello all

    I have build a list that contains data in the form below
    -- simplified for question --
    myList = [[value1, value2, value3],[value1, value2, value3], ...]

    I have a function which takes value3 from the lists above and returns
    another value. I want to use this returned value to sort the lists.

    So, my resultant list would be ordered by the return value of the
    function with value3 as its argument.

    From a relative Python newb, what is the best way to do this?

    Thanks for any help offered.


  • Paul Rubin

    #2
    Re: best Pythonic way to do this sort: Python newb

    "Sean Berry" <sean@buildingo nline.com> writes:[color=blue]
    > myList = [[value1, value2, value3],[value1, value2, value3], ...]
    >
    > I have a function which takes value3 from the lists above and returns
    > another value. I want to use this returned value to sort the lists.
    >
    > So, my resultant list would be ordered by the return value of the
    > function with value3 as its argument.
    >
    > From a relative Python newb, what is the best way to do this?[/color]

    def get_key(x): return x[2]
    sorted_list = sorted(myList, key=get_key)

    Comment

    • Sean Berry

      #3
      Re: best Pythonic way to do this sort: Python newb


      "Paul Rubin" <http://phr.cx@NOSPAM.i nvalid> wrote in message
      news:7xbr1y9e1c .fsf@ruckus.bro uhaha.com...[color=blue]
      > "Sean Berry" <sean@buildingo nline.com> writes:[color=green]
      >> myList = [[value1, value2, value3],[value1, value2, value3], ...]
      >>
      >> I have a function which takes value3 from the lists above and returns
      >> another value. I want to use this returned value to sort the lists.
      >>
      >> So, my resultant list would be ordered by the return value of the
      >> function with value3 as its argument.
      >>
      >> From a relative Python newb, what is the best way to do this?[/color]
      >
      > def get_key(x): return x[2]
      > sorted_list = sorted(myList, key=get_key)[/color]

      Sorry if I am missing something. But. what is sorted here?

      My simplified function looks like this

      def myFunction( myNumber ):
      "do some math calculations to myNumber"
      return "result of calculations"

      So, I want to sort myList by the return of myFunction( value3 )

      I tried doing the following... with no luck so far
      myList.sort(lam bda x, y: cmp(myFunction( x[2]), myFunction(y[2]))

      Thanks for any help.


      Comment

      • Paul Rubin

        #4
        Re: best Pythonic way to do this sort: Python newb

        "Sean Berry" <sean@buildingo nline.com> writes:[color=blue][color=green]
        > > def get_key(x): return x[2]
        > > sorted_list = sorted(myList, key=get_key)[/color]
        >
        > Sorry if I am missing something. But. what is sorted here?[/color]

        sorted is a built-in function that sorts the thing that you pass it.
        It just appeared in Python 2.4, I think. With older versions, yeah,
        you have to use the .sort method that sorts in place.
        [color=blue]
        > I tried doing the following... with no luck so far
        > myList.sort(lam bda x, y: cmp(myFunction( x[2]), myFunction(y[2]))[/color]

        That looks ok to me.
        [color=blue][color=green][color=darkred]
        >>> x = [(i,i*i,1 + 17*i**2 - i**3) for i in range(20)]
        >>> x[/color][/color][/color]
        [(0, 0, 1), (1, 1, 17), (2, 4, 61), (3, 9, 127), (4, 16, 209), (5, 25,
        301), (6, 36, 397), (7, 49, 491), (8, 64, 577), (9, 81, 649), (10,
        100, 701), (11, 121, 727), (12, 144, 721), (13, 169, 677), (14, 196,
        589), (15, 225, 451), (16, 256, 257), (17, 289, 1), (18, 324, -323),
        (19, 361, -721)][color=blue][color=green][color=darkred]
        >>> x.sort(lambda a,b:cmp(a[2],b[2]))
        >>> x[/color][/color][/color]
        [(19, 361, -721), (18, 324, -323), (0, 0, 1), (17, 289, 1), (1, 1,
        17), (2, 4, 61), (3, 9, 127), (4, 16, 209), (16, 256, 257), (5, 25,
        301), (6, 36, 397), (15, 225, 451), (7, 49, 491), (8, 64, 577), (14,
        196, 589), (9, 81, 649), (13, 169, 677), (10, 100, 701), (12, 144,
        721), (11, 121, 727)][color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Comment

        • Brett Hoerner

          #5
          Re: best Pythonic way to do this sort: Python newb

          (sorted is a built-in function in 2.4)

          def myFunction( data ):
          """ Take one of your set of 3, grab [2] (the 3rd) and do calcs,
          return value """
          "do some math calculations to data[2]"
          return "result of calculations"

          sorted_list = sorted(myList, key=myFunction)

          List is sorted in the order of the 'key' values, key being a value
          returned from myFunction which operates on [2].

          Comment

          • Satchidanand Haridas

            #6
            Re: best Pythonic way to do this sort: Python newb


            ----
            Satchidanand Haridas (sharidas at zeomega dot com)

            ZeOmega (www.zeomega.com)
            Open Minds' Open Solutions



            Sean Berry wrote:
            [color=blue]
            >"Paul Rubin" <http://phr.cx@NOSPAM.i nvalid> wrote in message
            >news:7xbr1y9e1 c.fsf@ruckus.br ouhaha.com...
            >
            >[color=green]
            >>"Sean Berry" <sean@buildingo nline.com> writes:
            >>
            >>[color=darkred]
            >>>myList = [[value1, value2, value3],[value1, value2, value3], ...]
            >>>
            >>>I have a function which takes value3 from the lists above and returns
            >>>another value. I want to use this returned value to sort the lists.
            >>>
            >>>So, my resultant list would be ordered by the return value of the
            >>>function with value3 as its argument.
            >>>
            >>>From a relative Python newb, what is the best way to do this?
            >>>
            >>>[/color]
            >>def get_key(x): return x[2]
            >>sorted_list = sorted(myList, key=get_key)
            >>
            >>[/color]
            >
            >Sorry if I am missing something. But. what is sorted here?
            >
            >My simplified function looks like this
            >
            >def myFunction( myNumber ):
            > "do some math calculations to myNumber"
            > return "result of calculations"
            >
            >So, I want to sort myList by the return of myFunction( value3 )
            >
            >I tried doing the following... with no luck so far
            >myList.sort(la mbda x, y: cmp(myFunction( x[2]), myFunction(y[2]))
            >
            >
            >[/color]
            I think the above statement should be as follows:

            myList.sort(lam bda x, y: cmp(myFunction( x[2]) - myFunction(y[2]))



            hope that helps.

            regards,
            Satchit

            Comment

            • George Sakkis

              #7
              Re: best Pythonic way to do this sort: Python newb

              "Satchidana nd Haridas" <sharidas@zeome ga.com> wrote:
              [color=blue][color=green]
              > >So, I want to sort myList by the return of myFunction( value3 )
              > >
              > >I tried doing the following... with no luck so far
              > >myList.sort(la mbda x, y: cmp(myFunction( x[2]), myFunction(y[2]))
              > >
              > >
              > >[/color]
              > I think the above statement should be as follows:
              >
              > myList.sort(lam bda x, y: cmp(myFunction( x[2]) - myFunction(y[2]))
              >
              >
              >
              > hope that helps.[/color]

              It would help more if you tested it before you posted. cmp takes two arguments (let alone that
              subtraction may not be defined for the list elements), so the original version is correct.

              George


              Comment

              Working...