Fast sorting many lists together...?

William Park parkw at better.net
Fri Sep 15 17:47:45 EDT 2000


On Fri, Sep 15, 2000 at 10:35:41PM +0200, Alex Martelli wrote:
> If I understand you correctly, what you want is very well offered
> by Numerical Python (which is not only for numerical stuff: its
> arrays can hold generic Python objects).  If you do need to have
> your stuff in lists, then moving it back and forth list<->array
> might be slow, though.
> 
> So, as mentioned on another thread (yesterday, I think): zip
> the list your sorting with the indices, then use those.  It's easier
> in Python 2.0, currently in beta:
> 
> def sortone_changemany(firstlist, *theothers):
>     zipped = zip(firstlist, range(len(firstlist)))
>     zipped.sort()
>     results = [ [i for i, j in zipped] ]
>     indices = [j for i, j in zipped]
>     for alist in theothers:
>         results.append([alist[j] for j in indices])
>     return results
> 
> With older Pythons, you can use map instead of zip, and more
> explicit loops instead of the list comprehensions, but the
> general idea is similar.
> 
> 
> Alex

Thanks.  Right now, I'm using a standard Python solution where you
create a list of tuples.  It's just that a faster routine would be
helpful.

I'll look into NumPy.

--William




More information about the Python-list mailing list