473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1989

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
1
by: Wensheng | last post by:
Hi, I wrote a small template engine called spytee. Like any template enigne, it take a text(html) template file as input, process the variable tags in the file, and display the resulted text. The...
1
by: Brian Basquille | last post by:
Hello all. Have been working on the Air Hockey game on and off for a couple of weeks now.. but have had plenty of other assignments to keep me busy along with it. But i would like some...
1
by: Robin | last post by:
For an asp.net project that is deployed to a load balanced web servers, are there any performance changes that can be made in .Net runtime or IIS 6? Also are there any additional tips for...
2
by: Irfan Akram | last post by:
Hi Guys, I am in search of some innovative ideas in improving the interface of Online Marking System. At the moment it has some good functionality, but lacks a professional interface. Is...
0
by: Umut Tezduyar | last post by:
I really hate working with .net framework design time attributes. I have read asp.net server controls and components (Nikhil Kothari, Vandana Datye) book, chapter 15 but still i can't handle the...
1
by: Larry Neylon | last post by:
Hi, I'm working on a VBScript application on IIS6 and I'm looking for some advice about the best way of replacing or improving session variable usage. The application is in a secure extranet...
1
by: mjheitland | last post by:
Hello, does anyone know if an update is available (or at least planned) for the much cited standard reference IMPROVING .NET APPLICATION PERFORMANCE AND SCALABILITY? link:...
10
by: Jo | last post by:
Hi there: I m wondering what can I do to improve my code, everytime I am coding I feel like it could be done better. I started on c# a good months ago and feel conformtable but sometimes I Need...
3
by: Jeff | last post by:
..NET 2.0 I've created a window consisting of a treeview control and a tabcontrol. When a leave in the treeview is clicked then the code should check if the specific tabpage already exists in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.