Efficient python 2-d arrays?

Jake Biesinger jake.biesinger at gmail.com
Mon Jan 17 20:30:55 EST 2011


> IIUC (please confirm), you don't need a generic two-dimensional
> array, but rather an Nx2 array, where N may be large (but the other
> dimension will always have a magnitude of 2).

Yes, that's right, Nx2 not NxM.

> > Since I want to keep the two elements together during a sort
> 
> I assume (please confirm) that you want to sort by one of the numbers,
> and keep the other one together with the sort key.

Again, yes.
 
> I suggest that you implement your own sorting algorithm, or reuse
> heapsort (actually, any of the sorting algorithms would do).
> 
> You then use array.array, and take the elements at indices 2k and 2k+1
> together. Sorting would then only look at even indices, but any swapping
> you do during the sorting would always swap two numbers with two other
> numbers.

Yes, this is an option. Writing a custom sort function just didn't sound very appealing and I was hoping for a more generic solution.  

Thanks!



More information about the Python-list mailing list