Efficient python 2-d arrays?

Martin v. Loewis martin at v.loewis.de
Mon Jan 17 20:11:16 EST 2011

> Using numpy, I can create large 2-dimensional arrays quite easily.

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).

> 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.

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


More information about the Python-list mailing list