[Tutor] sorting data from multiple arrays
R. Alan Monroe
amonroe at columbus.rr.com
Thu Mar 22 20:13:12 CET 2007
> hello, I typically run into this problem and I'm not always sure of the most efficient way to handle it. I often work with multiple arrays of data, say arrays a, b, and c, and I want to sort the
> elements of b and c based on a. for example:
> a = [3,2,1,4]
> b = ['hi', 'my','name', 'is']
> c = [5,2,4,2]
> I order 'a' from small to large and do the same rearrangement to 'b' and 'c':
> a = [1,2,3,4]
> b = ['name', 'my','hi', 'is']
> c = [4,2,5,2]
> usually I do some terrible looking for loops and iterate over the whole mess.... is there a clean, efficient way to do this, or is there a nice function that would reorder the elements of b and c
> based on the soring of a?
Is switching to a dictionary an option?
mystuff = {3: ('hi', 5),
2: ('my', 2),
1: ('name', 4),
4: ('is', 2) }
You can get a list of the keys, sort _that_, then fetch from the dict
based on the sorted list.
Alan
More information about the Tutor
mailing list