470,563 Members | 2,427 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,563 developers. It's quick & easy.

Improving upon the Decorate-Sort-Undecorate idiom

I recently had the need to sort a large number of lists of lists, and
wondered if an improvement to the Decorate-Sort-Undecorate idiom is in
the works. Ideally, I would like to sort the list of lists (or tuples)
in place by using a simple variant of the current idiom, i.e.

list_of_lists.sort(*columns)

where *columns is a tuple that specifies the column order for the
sort. If *columns is left blank, the sort ought to work as it does
today, i.e.

list_of_lists.sort()

should sort by columns 0, 1,2,.....

Has such a method been considered for inclusion in Python? Does it
have any problems that would inhibit its adoption?

Thomas Philips
Jul 18 '05 #1
2 1857

"Thomas Philips" <tk****@hotmail.com> wrote in message
news:b4**************************@posting.google.c om...
I recently had the need to sort a large number of lists of lists, and
wondered if an improvement to the Decorate-Sort-Undecorate idiom is in
the works.


See What's New for 2.4 at python.org.
Jul 18 '05 #2
"Thomas Philips" <tk****@hotmail.com> wrote in message
news:b4**************************@posting.google.c om...
I recently had the need to sort a large number of lists of lists, and
wondered if an improvement to the Decorate-Sort-Undecorate idiom is in
the works. Ideally, I would like to sort the list of lists (or tuples)
in place by using a simple variant of the current idiom, i.e.

list_of_lists.sort(*columns)

where *columns is a tuple that specifies the column order for the
sort. If *columns is left blank, the sort ought to work as it does
today, i.e.

list_of_lists.sort()

should sort by columns 0, 1,2,.....

Has such a method been considered for inclusion in Python? Does it
have any problems that would inhibit its adoption?

Thomas Philips

Thomas -

Here are some pure-Python ideas, plus a preview of the Python 2.4 sort()
enhancement.

-- Paul
import pprint
listOfLists = [
[ 'a', 1, 'z', 3.1 ],
[ 'c', 0, 'z', 4.2 ],
[ 'a', 1, 'y', 5.5 ],
[ 'b', 2, 'z', 1.0 ],
[ 'c', 0, 'z', 4.2 ],
]

print "\n- original list"
pprint.pprint (listOfLists)

print "\n- vanilla sort()"
listOfLists.sort()
pprint.pprint (listOfLists)

def byColumns(col):
def columnCompare(a,b):
for c in col:
if a[c] != b[c]:
return cmp(a[c],b[c])
else:
return 0
return columnCompare

print "\n- using custom sort method"
columns = (1, 2, 0)
listOfLists.sort(byColumns(columns))
pprint.pprint (listOfLists)

def sortByColumns(lst,cols):
tmp = [ ([item[c] for c in cols],item) for item in lst ]
tmp.sort()
return [ t[1] for t in tmp]

print "\n- using D-S-U with varying input columns"
columns = (2,3)
listOfLists = sortByColumns(listOfLists, columns)
pprint.pprint (listOfLists)

print "\n- using key argument in Python 2.4 (compare to D-S-U)"
cols = (1,3)
listOfLists.sort( key=lambda item: [item[c] for c in (cols)] )
pprint.pprint (listOfLists)
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

41 posts views Thread by John Marshall | last post: by
1 post views Thread by Brian Basquille | last post: by
1 post views Thread by Robin | last post: by
2 posts views Thread by Irfan Akram | last post: by
reply views Thread by Umut Tezduyar | last post: by
10 posts views Thread by Jo | last post: by
3 posts views Thread by Jeff | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.