Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

Sorting two dimentional array by column?

 
Jump to:  
 
Tobiah
PostPosted: 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
PostPosted: 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
PostPosted: 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
PostPosted: 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
PostPosted: 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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Makumba - Big Cyc pralki Kawały Dieta program do wystawiania faktur