sorting many arrays from one...

Alex Martelli aleax at aleax.it
Tue Jul 9 09:53:55 EDT 2002


Shagshag13 wrote:

> 
> "Alex Martelli" <aleax at aleax.it> a écrit dans le message de news:
> nYAW8.59639$vm5.2174555 at news2.tin.it...
> 
>> aux_list = zip(drive, other1, other2, other3)
>> aux_list.sort()
>>
>> for i in range(len(drive)):
>>     drive[i], other1[i], other2[i], other3[i] = aux_list[i]
>>
> 
> thanks ! it's quite impressive and even works with array (i didn't even
> knew this "zip" built-in function nor thought that sorting was possible on
> tuples). I think that if you don't use "map" to replace the "for loop",
> it's because it's impossible, so i don't need to try ;o)

No, as Duncan just showed in another post -- you can just use zip the
other way 'round (zip(*aux_list)) to give the right sequences again (though
you then also need map or a list comprehension to make the tuples into
lists).  I had just coded this in the simplest way that came to mind,
without any special effort to do clever things (and that zip is in a
sense its own inverse IS pretty clever indeed:-).


> i have another question, i need a way to have a one to one mapping from
> string to integer to save storage : in fact having an id key associated
> with strings (like in a database). 

I'm not sure how that would save you any storage at all.  Since the
strings DO need to be stored somewhere anyway, what's saved by using
their id's rather than just references to them, as Python usually does?
I don't think you're saving any storage at all with all the code below.
Why not just try using a simple dictionary vs this approach (with some
HUGE number of keys to make sure) and use platform dependent ways,
e.g. program 'top' under Linux, to check VM usage in either case?

> and again great thanks for all your advices / replies...

You're welcome!


Alex




More information about the Python-list mailing list