Sorting two dimentional array by column?

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

    Sorting two dimentional array by column?

    Imagine an excel spreadsheet. I can choose
    a column and sort the records based on the items
    in that column. I would like to do the same
    thing with a large two dimensional array.
    What would be the fastest way (in computation time)
    to accomplish this?

    This seems similar to a recent sorting thread,
    but with that one, the 'master' table was
    sequential integers. This array would be
    filled with arbitrary data.

    Thanks
    ** Posted from http://www.teranews.com **
  • Tobiah

    #2
    Re: Sorting two dimentional array by column?


    Imagine an excel spreadsheet. I can choose
    a column and sort the records based on the items
    in that column. I would like to do the same
    thing with a large two dimensional array.
    What would be the fastest way (in computation time)
    to accomplish this?
    Now that I think about the problem more, I really want
    to sort an array of dictionaries according one of the
    keys of each. Could I use:

    array.sort(key = something)

    Where something looks at the proper item in the
    current row? I can't quite visualize how to pull
    the item out of the dictionary.

    Thanks

    ** Posted from http://www.teranews.com **

    Comment

    • Paddy

      #3
      Re: Sorting two dimentional array by column?

      On Jul 2, 10:35 pm, Tobiah <t...@tobiah.or gwrote:
      Imagine an excel spreadsheet.  I can choose
      a column and sort the records based on the items
      in that column.  I would like to do the same
      thing with a large two dimensional array.
      What would be the fastest way (in computation time)
      to accomplish this?
      >
      Now that I think about the problem more, I really want
      to sort an array of dictionaries according one of the
      keys of each.  Could I use:
      >
      array.sort(key = something)
      >
      Where something looks at the proper item in the
      current row?  I can't quite visualize how to pull
      the item out of the dictionary.
      >
      Thanks
      >
      ** Posted fromhttp://www.teranews.co m**
      Hi Tobiah,
      Try this:

      arrayofdicts.so rt(
      key = lambda dictinarray: dictinarray.get (sortkeyname)
      )

      - Paddy.

      Comment

      • Tobiah

        #4
        Re: Sorting two dimentional array by column?

        >Imagine an excel spreadsheet. I can choose
        >a column and sort the records based on the items
        >in that column. I would like to do the same
        >thing with a large two dimensional array.
        >What would be the fastest way (in computation time)
        >to accomplish this?
        >
        Now that I think about the problem more, I really want
        to sort an array of dictionaries according one of the
        keys of each. Could I use:
        >
        array.sort(key = something)
        >
        Where something looks at the proper item in the
        current row? I can't quite visualize how to pull
        the item out of the dictionary.
        Sorry to reply to myself so many times, but I have come
        up with the answer:

        from operator import itemgetter

        thing = [
        {'animal': 'duck', 'tool': 'pond'},
        {'animal': 'dog', 'tool': 'bone'},
        {'animal': 'bear', 'tool': 'hive'}
        ]

        get = itemgetter('ani mal')
        thing.sort(key = get)

        print thing

        *************** *************** ***********

        [
        {'tool': 'hive', 'animal': 'bear'},
        {'tool': 'bone', 'animal': 'dog'},
        {'tool': 'pond', 'animal': 'duck'}
        ]

        ** Posted from http://www.teranews.com **

        Comment

        • John Machin

          #5
          Re: Sorting two dimentional array by column?

          On Jul 3, 7:35 am, Tobiah <t...@tobiah.or gwrote:
          Imagine an excel spreadsheet. I can choose
          a column and sort the records based on the items
          in that column. I would like to do the same
          thing with a large two dimensional array.
          What would be the fastest way (in computation time)
          to accomplish this?
          >
          Now that I think about the problem more, I really want
          to sort an array of dictionaries according one of the
          keys of each. Could I use:
          >
          array.sort(key = something)
          >
          Where something looks at the proper item in the
          current row? I can't quite visualize how to pull
          the item out of the dictionary.
          Manual sez: """key specifies a function of one argument that is used
          to extract a comparison key from each list element: "key=str.lo wer"
          """

          Assuming that "sort an array of dictionaries according one of the keys
          of each" means "sort an array of dictionaries according to the value
          stored for one of the keys of each".

          If the dict key were a constant, you could do:

          array.sort(key= lambda adict: adict['the_constant_k ey'])

          However you want the current row, and sort wants a 1-arg key
          function ... to stick with the key= caper you'll need to curry the
          extra arg. So (Python 2.5 onwards):

          # do this once
          import functools
          def dict_extract(ad ict, akey):
          return adict[akey]

          # do this each time you want to sort
          current_row_key = whatever()
          sort_key_func = functools.parti al(dict_extract , akey=current_ro w_key)
          array.sort(key= sort_key_func)

          Alternatively, use the decorate-sort-undecorate procedure:

          temp = [(d[current_row_key], d) for d in array]
          temp.sort()
          array[:] = [tup[1] for tup in temp]

          HTH,
          John

          Comment

          Working...