sorting many arrays from one...

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Jul 9 09:29:18 EDT 2002


Alex Martelli <aleax at aleax.it> wrote in 
news:nYAW8.59639$vm5.2174555 at news2.tin.it:

> Shagshag13 wrote:
> 
>> hello,
>> 
>> i'm looking for an "efficient" way of sorting many arrays driven by one,
>> for example
>> 
>> drive = [1, 4, 2, 3]
>> other1 = [a, b, c, d]
>> other2 = [ k, j, h, l ]
>> other3 = [14, 18, 19, 11]
>> 
>> and i get :
>> 
>> drive = [1, 2, 3, 4]
>> other1 = [a, c, d, b]
>> other2 = [ k, h, l, j]
>> other3 = [14, 19, 11, 18]
> 
> aux_list = zip(drive, other1, other2, other3)
> aux_list.sort()
> 
Fine so far.

> for i in range(len(drive)):
>     drive[i], other1[i], other2[i], other3[i] = aux_list[i]
A shorter way is:

  drive, other1, other2, other3 = map(list, zip(*aux_list))

and if you don't mind them turning into tuples you can even do:

  drive, other1, other2, other3 = zip(*aux_list)

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list