sorting a list of tupples

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

    sorting a list of tupples

    I have a list of tupples. Each tupple has 3 items. The first item is
    an integer. I'd like to sort the list(of tupples), based on the first
    element of the tupple. I thought about stringifying the tupples, then sorting
    those, but thought there must be a better way. any idea's?

    --David Bear
    phone: 480-965-8257
    fax: 480-965-9189
    College of Public Programs/ASU
    Wilson Hall 232
    Tempe, AZ 85287-0803
    "Beware the IP portfolio, everyone will be suspect of trespassing"
  • John Roth

    #2
    Re: sorting a list of tupples


    "David Bear" <david.bear@asu .edu> wrote in message
    news:m3ptgegrp6 .fsf@moroni.pp. asu.edu...[color=blue]
    > I have a list of tupples. Each tupple has 3 items. The first item is
    > an integer. I'd like to sort the list(of tupples), based on the first
    > element of the tupple. I thought about stringifying the tupples, then[/color]
    sorting[color=blue]
    > those, but thought there must be a better way. any idea's?[/color]

    <list>.sort() should do the job, as long as you want the result
    to be a list in ascending order by the value of the first element
    of the tuple.

    John Roth
    [color=blue]
    >
    > --David Bear
    > phone: 480-965-8257
    > fax: 480-965-9189
    > College of Public Programs/ASU
    > Wilson Hall 232
    > Tempe, AZ 85287-0803
    > "Beware the IP portfolio, everyone will be suspect of trespassing"[/color]


    Comment

    • Daniel Luz

      #3
      Re: sorting a list of tupples

      David Bear wrote:
      [color=blue]
      > I have a list of tupples. Each tupple has 3 items. The first item is
      > an integer. I'd like to sort the list(of tupples), based on the first
      > element of the tupple. I thought about stringifying the tupples, then sorting
      > those, but thought there must be a better way. any idea's?[/color]

      Have you tried just sort()ing the list? Tuples naturally will sort based
      on the first element of them (then, if two elements are equal, it will
      try to sort based on the second element, and so forth).

      [color=blue][color=green][color=darkred]
      >>> l = [(1, 'p'), (6, 'n'), (4, 'h'), (3, 't'), (5, 'o'), (2, 'y')]
      >>> l.sort()
      >>> l[/color][/color][/color]
      [(1, 'p'), (2, 'y'), (3, 't'), (4, 'h'), (5, 'o'), (6, 'n')]


      If that's not what you meant, could you please clarify?


      --
      Daniel

      Comment

      • Stephen Horne

        #4
        Re: sorting a list of tupples

        On 30 Oct 2003 17:45:57 -0700, David Bear <david.bear@asu .edu> wrote:
        [color=blue]
        >I have a list of tupples. Each tupple has 3 items. The first item is
        >an integer. I'd like to sort the list(of tupples), based on the first
        >element of the tupple. I thought about stringifying the tupples, then sorting
        >those, but thought there must be a better way. any idea's?[/color]

        You can sort a list of tuples using the standard sort method. Ordering
        of tuples is derived much like ordering of strings - it will use the
        order of the first items in the tuples unless they are equal in which
        case it uses the second items, or third items, or whatever. If all
        else fails, the longest tuple is considered greater.

        If you are sorting on the first item, the sort method should just work
        for you.


        --
        Steve Horne

        steve at ninereeds dot fsnet dot co dot uk

        Comment

        • Dave Kuhlman

          #5
          Re: sorting a list of tupples

          John Roth wrote:
          [color=blue]
          >
          > "David Bear" <david.bear@asu .edu> wrote in message
          > news:m3ptgegrp6 .fsf@moroni.pp. asu.edu...[color=green]
          >> I have a list of tupples. Each tupple has 3 items. The first
          >> item is
          >> an integer. I'd like to sort the list(of tupples), based on the
          >> first
          >> element of the tupple. I thought about stringifying the tupples,
          >> then[/color]
          > sorting[color=green]
          >> those, but thought there must be a better way. any idea's?[/color]
          >
          > <list>.sort() should do the job, as long as you want the result
          > to be a list in ascending order by the value of the first element
          > of the tuple.[/color]

          And, if you don't want to sort on the first element or want to
          sort in descending order or ... you will want to know about the
          optional argument to the sort method. You can pass it your own
          custom comparison function. Read about it here:



          The info you (might) want is down in a footnote.

          Dave

          --
          Dave Kuhlman

          dkuhlman@rexx.c om

          Comment

          Working...