|  | Sorting two dimentional array by column? |  | |
| | | Tobiah |  |
| Posted: Wed Jul 02, 2008 7:25 pm Post subject: 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 LINK ** |
| |
| | | Tobiah |  |
| Posted: Wed Jul 02, 2008 7:35 pm Post subject: Re: Sorting two dimentional array by column? |  |
| Quote: | 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 LINK ** |
| |
| | | Tobiah |  |
| Posted: Wed Jul 02, 2008 8:22 pm Post subject: Re: Sorting two dimentional array by column? |  |
| |  | |
| Quote: | 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 LINK ** |
| |
| | | Paddy |  |
| Posted: Wed Jul 02, 2008 10:09 pm Post subject: Re: Sorting two dimentional array by column? |  |
On Jul 2, 10:35 pm, Tobiah <t...@tobiah.org> wrote:
| Quote: | 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 LINK**
|
Hi Tobiah, Try this:
arrayofdicts.sort( key = lambda dictinarray: dictinarray.get(sortkeyname) )
- Paddy. |
| |
| | | John Machin |  |
| Posted: Wed Jul 02, 2008 10:29 pm Post subject: Re: Sorting two dimentional array by column? |  |
| |  | |
On Jul 3, 7:35 am, Tobiah <t...@tobiah.org> wrote:
| Quote: | 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 |
| |
|
|