alwayssortedlist (was Re: On PEP 322 (ireverse))
Jeremy Fincher
tweedgeezer at hotmail.com
Thu Oct 30 14:17:35 EST 2003
Alex Martelli <aleax at aleax.it> wrote in message news:<HaVnb.65220$e5.2392011 at news1.tin.it>...
> Perfectly right. If you DO want a listoid container that keeps its
> elements sorted, that's not hard to make for yourself. There are
> basically two obvious implementation strategies:
> -- ensure the underlying list is re-sorted at each insertion,
> append &c, and/or
> -- let the underlying list grow as it will on insertion &c, but
> make sure it's sorted at any method that accesses it (you can
> use a flag -- 'have there being any append/insert/... since
> the last sort?' -- to re-sort only when needed).
>
> I'll choose the first for general use because:
> -- accesses are much more frequent than modifications,
> -- list.sort is VERY fast when a list is "nearly sorted"
> -- the second approach would require sorting also on many
> kind of changes (e.g. __setitem__ -- to ensure the items
> being replaced ARE those the user expects...)
> &c.
Any reason he shouldn't just use bisect.insort?
Jeremy
More information about the Python-list
mailing list