[Tutor] Sorting more than one list
Max Noel
maxnoel_fr at yahoo.fr
Thu Mar 31 01:20:15 CEST 2005
On Mar 31, 2005, at 04:19, Diego Galho Prestes wrote:
> Hi!
>
> I need to sort 4 lists but I need that they make the "sort together".
> I'll sort just one but when I change the position of the items of the
> 1st list I have to change the positions of the other 3 lists. Can I do
> this just using the sort() method of the list object?
> If I can't, someone know a site that have one sort method in python
> that
> its easily to implement and fast?
>
> Tks,
> Diego
You can do that with zip().
>>> zip([1, 2, 3], [3, 2, 1])
[(1, 3), (2, 2), (3, 1)]
It works with any number of lists. If you put the list you want to
sort against as the first argument to zip, all you have to do is to
then sort() the resulting list of tuples, and to "unzip" it, which
should be trivial.
>>> a = zip([3, 2, 1], [1, 2, 3])
>>> a
[(3, 1), (2, 2), (1, 3)]
>>> a.sort()
>>> a
[(1, 3), (2, 2), (3, 1)]
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting
and sweating as you run through my corridors... How can you challenge a
perfect, immortal machine?"
More information about the Tutor
mailing list