[Tutor] Re: how to do a (special) sorted list

gyro funch gyromagnetic@excite.com
Tue Apr 1 08:32:01 2003

alan.gauld@bt.com wrote:
>>The second problem is to implement a sorted list with the 
>>following options:
> It looks like a standard Python list with the twist of sorting itself 
> after every addition.(Which could become slow BTW!)
> You could create a class to do that either subclassing from the standard 
> list or, I think thee is a UserList which is specifically designed for 
> this. Then override the insert/append/assign operations to call the 
> standard variant followed by a sort() call.
> Alan g
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Check out the 'bisect' module. It contains functions that can insert items in a list in such a way as to maintain sorted order.

I expect that an initial sort (using Python's very fast sort implementation), followed by inserts using bisect.insort() wouldn't be too shabby.


Join Excite! - http://www.excite.com
The most personalized portal on the Web!