From: Tobiah on
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 **
From: Tobiah on


> 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 **
From: Paddy on
On Jul 2, 10:35 pm, Tobiah <t...(a)tobiah.org> wrote:
> > 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.com**

Hi Tobiah,
Try this:

arrayofdicts.sort(
key = lambda dictinarray: dictinarray.get(sortkeyname)
)

- Paddy.
From: Tobiah on

>> 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('animal')
thing.sort(key = get)

print thing

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

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

** Posted from http://www.teranews.com **
From: John Machin on
On Jul 3, 7:35 am, Tobiah <t...(a)tobiah.org> wrote:
> > 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.lower"
"""

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_key'])

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(adict, akey):
return adict[akey]

# do this each time you want to sort
current_row_key = whatever()
sort_key_func = functools.partial(dict_extract, akey=current_row_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