Fast sorting many lists together...?
William Park
parkw at better.net
Fri Sep 15 14:58:19 EDT 2000
On Fri, Sep 15, 2000 at 02:16:54PM -0400, Mike Fletcher wrote:
> Hmm, you might try something like...
>
> tuples = apply( map, (None, )+tuple( list-of-lists) )
> # tuples is now a list of "records"
> tuples.sort()
>
> Then just pull the columns back out again with a for loop. You'd put
> list-of-lists in the order you want the keys to sort, so primary key first,
> then secondary, etc. That's only 25000 new tuples, which I would think
> would be fairly fast on most machines.
>
> Alternately, just do a map(None, keycol, range(len(keycol))) to get key,
> index tuples, then sort that and use a for loop to pick the indices out of
> each column.
>
> HTH,
> Mike
Hi Mike. I'm currently doing 2nd method, and it's slow. As you
correctly surmised, my situation is similar to sorting lines where
fields are lists elements.
The problem is that I sometimes need to sort one field in reverse order.
For this, I'm using 'listsort(mycmp)' where 'mycmp()' is a function
which returns -1,0,1 depending on what I want. This is also slow.
--William
More information about the Python-list
mailing list