473,796 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.s ort(*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.s ort()

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 2011

"Thomas Philips" <tk****@hotmail .com> wrote in message
news:b4******** *************** ***@posting.goo gle.com...
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.goo gle.com...
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.s ort(*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.s ort()

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.sor t()
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.sor t(byColumns(col umns))
pprint.pprint (listOfLists)

def sortByColumns(l st,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(l istOfLists, columns)
pprint.pprint (listOfLists)

print "\n- using key argument in Python 2.4 (compare to D-S-U)"
cols = (1,3)
listOfLists.sor t( 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
2880
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
1627
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 difference from most templates are: you can edit the template file in the html editor(Frontpage, Dreamweaver, whatever), thus a near Complete seperation of presentation and logic. It's very simple and easy, you only need 3 types of tags to...
1
1350
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 suggestions on improving it. It's far from fully working, but some suggestions on the following would be great. Please download it from: http://homepage.eircom.net/~basquilletj/AirHockey.rar.
1
1148
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 reviewing the asp.net (VB) code for improving performance?
2
1167
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 there a way of improving the whole interface of the system to make it look consistent as well as professional. Any useful links will be appreciated! Also, how can we copy incorporate the same layout of an existing web-site in
0
1066
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 complex tasks on designer. The thing i want to do is so simple (I hope so). I want my control to understand the Menu and SubMenus under Menu. Ex: <cc1:MyConrol runat="server" id="MyControl1"> <Menu name="level1"> <SubMenus> <Menu name="level2">
1
2002
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 environment. Currently the application has a search customers page with 10 search fields which list the results below the search fields. The requirement for this screen was that the user could return to this result page at any point from any page...
1
1524
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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenet.asp or as a download:
10
1785
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 to look up stuff. maybe someone can suggest a good book on good practices for c#? Cheers Jo
3
1546
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 the tabcontrol and give it focus if it exists. Or else if it doesn't exists then add the tagpage to the tabcontrol. Below is the code which checks if a tabpage already exists and if it does then give it focus... If you have a suggestion on...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.