Doubt regarding sorting of a list specific field

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

    #1

    Doubt regarding sorting of a list specific field

    Dear All,

    I have doubt regarding sorting. I have a list
    that list have another list (eg)

    list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

    I want to sort only numeric value having array field.
    How I need to do for that.

    with regards
    Prabahar






    _______________ _______________ _______________ _______________ ____________
    Yahoo! India Matrimony: Find your life partner online
    Go to: http://yahoo.shaadi.com/india-matrimony
  • vincent wehren

    #2
    Re: Doubt regarding sorting of a list specific field

    "praba kar" <prabapython@ya hoo.co.in> schrieb im Newsbeitrag
    news:mailman.18 17.1113371391.1 799.python-list@python.org ...
    | Dear All,
    |
    | I have doubt regarding sorting. I have a list
    | that list have another list (eg)
    |
    | list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

    -> Be careful, 0432 is octal notation for 282.
    |
    | I want to sort only numeric value having array field.
    | How I need to do for that.

    You may want use the decorate-sort-undecorate idiom.
    I othere words, you isolate the index you want to sort by,
    sort on the indices, than get rid of the indices again.

    Something like:

    def sortSeqOfSeqs(s eq, idx):
    tmp = sorted([(elem[idx], elem) for elem in seq])
    return [elem[1] for elem in tmp]

    seq = [[1234,'name1'],[2234,'name2'],[1432,'name3']]
    print sortSeqOfSeqs(s eq, 0)
    # prints [[1234, 'name1'], [1432, 'name3'], [2234, 'name2']]
    # Or to sort by the name index
    print sortSeqOfSeqs(s eq, 1)
    # prints [[1234, 'name1'], [2234, 'name2'], [1432, 'name3']]


    Is this what you we're looking for?

    --

    Vincent Wehren








    |
    | with regards
    | Prabahar
    |
    |
    |
    |
    |
    |
    | _______________ _______________ _______________ _______________ ____________
    | Yahoo! India Matrimony: Find your life partner online
    | Go to: http://yahoo.shaadi.com/india-matrimony


    Comment

    • Steven Bethard

      #3
      Re: Doubt regarding sorting of a list specific field

      praba kar wrote:[color=blue]
      > I have doubt regarding sorting. I have a list
      > that list have another list (eg)
      >
      > list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]
      >
      > I want to sort only numeric value having array field.
      > How I need to do for that.[/color]

      In Python 2.4:

      py> import operator
      py> seq = [(1234,'name1'), (2234,'name2'), (1432,'name3')]
      py> seq.sort(key=op erator.itemgett er(0))
      py> seq
      [(1234, 'name1'), (1432, 'name3'), (2234, 'name2')]

      Note that I've changed your name 'list' to 'seq' since 'list' is
      shadowing the builtin list function in your code, and I've changed your
      nested lists to tuples because they look like groups of differently
      typed objects, not sequences of similarly typed objects. See:



      That said, the code I've given will still work if you shadow the builtin
      list or if you use lists instead of tuples.

      STeVe

      Comment

      • F. Petitjean

        #4
        Re: Doubt regarding sorting of a list specific field

        Le Wed, 13 Apr 2005 01:13:40 -0600, Steven Bethard a écrit :[color=blue]
        > praba kar wrote:[color=green]
        >> list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]
        >>
        >> I want to sort only numeric value having array field.
        >> How I need to do for that.[/color]
        >
        > In Python 2.4:
        >
        > py> import operator
        > py> seq = [(1234,'name1'), (2234,'name2'), (1432,'name3')]
        > py> seq.sort(key=op erator.itemgett er(0))
        > py> seq
        > [(1234, 'name1'), (1432, 'name3'), (2234, 'name2')]
        >
        > Note that I've changed your name 'list' to 'seq' since 'list' is
        > shadowing the builtin list function in your code, and I've changed your
        > nested lists to tuples because they look like groups of differently
        > typed objects, not sequences of similarly typed objects. See:
        >
        > STeVe[/color]
        nice explaination. But, if the items of the list are tuples the default
        rule for comparing tuples is such that
        seq.sort()
        would be sufficient here.

        Comment

        • Steven Bethard

          #5
          Re: Doubt regarding sorting of a list specific field

          vincent wehren wrote:[color=blue]
          > "praba kar" <prabapython@ya hoo.co.in> schrieb im Newsbeitrag
          > news:mailman.18 17.1113371391.1 799.python-list@python.org ...
          > | Dear All,
          > |
          > | I have doubt regarding sorting. I have a list
          > | that list have another list (eg)
          > |
          > | list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]
          >
          > -> Be careful, 0432 is octal notation for 282.
          > |
          > | I want to sort only numeric value having array field.
          > | How I need to do for that.
          >
          > You may want use the decorate-sort-undecorate idiom.
          > I othere words, you isolate the index you want to sort by,
          > sort on the indices, than get rid of the indices again.
          >
          > Something like:
          >
          > def sortSeqOfSeqs(s eq, idx):
          > tmp = sorted([(elem[idx], elem) for elem in seq])
          > return [elem[1] for elem in tmp][/color]

          Note that this is not stable:

          py> seq = [(1,'c'),(1,'b')]
          py> [tup[1] for tup in sorted((x[0], x) for x in seq)]
          [(1, 'b'), (1, 'c')]

          Which should not have reordered the list since 1 == 1. If you want a
          stable version, you could use:

          py> [tup[2] for tup in sorted((x[0], i, x) for i, x in enumerate(seq))]
          [(1, 'c'), (1, 'b')]

          But that seems pretty silly. You're already using Python 2.4 features
          (e.g. sorted), so you might as well use the key argument to sorted:

          py> sorted(seq, key=operator.it emgetter(0))
          [(1, 'c'), (1, 'b')]

          Note if you're not using 2.4, you may want to use the
          decorate-sort-undecorate pattern like:

          py> decorated_seq = [(x[0], i, x) for i, x in enumerate(seq)]
          py> decorated_seq.s ort()
          py> [tup[2] for tup in decorated_seq]
          [(1, 'c'), (1, 'b')]

          But personally, I'd just upgrade. ;)

          STeVe

          Comment

          • Steven Bethard

            #6
            Re: Doubt regarding sorting of a list specific field

            F. Petitjean wrote:[color=blue]
            > Le Wed, 13 Apr 2005 01:13:40 -0600, Steven Bethard a écrit :
            >[color=green]
            >>praba kar wrote:
            >>[color=darkred]
            >>>list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]
            >>>
            >>>I want to sort only numeric value having array field.
            >>>How I need to do for that.[/color]
            >>
            >>In Python 2.4:
            >>
            >>py> import operator
            >>py> seq = [(1234,'name1'), (2234,'name2'), (1432,'name3')]
            >>py> seq.sort(key=op erator.itemgett er(0))
            >>py> seq
            >>[(1234, 'name1'), (1432, 'name3'), (2234, 'name2')]
            >>
            >>Note that I've changed your name 'list' to 'seq' since 'list' is
            >>shadowing the builtin list function in your code, and I've changed your
            >>nested lists to tuples because they look like groups of differently
            >>typed objects, not sequences of similarly typed objects. See:
            >>
            >>STeVe[/color]
            >
            > nice explaination. But, if the items of the list are tuples the default
            > rule for comparing tuples is such that
            > seq.sort()
            > would be sufficient here.[/color]

            See my other post in this thread. seq.sort() is not stable; if the OP's
            list looked like:
            [(1234,'name3'), (1234,'name1')]
            then calling seq.sort() would give
            [(1234,'name1'), (1234,'name3')]
            instead of
            [(1234,'name3'), (1234,'name1')]
            which is what I assumed the OP wanted when he said "I want to sort only
            numeric value".

            STeVe

            Comment

            • Steven Bethard

              #7
              Re: Doubt regarding sorting of a list specific field

              Steven Bethard wrote:[color=blue]
              > See my other post in this thread. seq.sort() is not stable;[/color]

              Hmmm... That didn't come out right. list.sort *is* stable. It's just
              that if you want to sort only by the first element of each tuple in a
              list, then list.sort won't produce results that are stable under this
              criterion.

              STeVe

              Comment

              Working...