sort array, apply rearrangement to second
Alf P. Steinbach
alfps at start.no
Tue Mar 30 19:45:35 EDT 2010
* Victor Eijkhout:
> I have two arrays, made with numpy. The first one has values that I want
> to use as sorting keys; the second one needs to be sorted by those keys.
> Obviously I could turn them into a dictionary of pairs and sort by the
> first member, but I think that's not very efficient, at least in space,
> and this needs to be done as efficiently as possible.
>
> I could use a hand.
Just do the pairing, but in a 'list', not a dictionary (a dictionary is
unordered and can't be sorted). You need to keep track of which keys belong to
which values anyway. And anything in Python is a reference: you're not copying
the data by creating the pairs. That is, the space overhead is proportional to
the number of items but is independent of the data size of each item.
Cheers & hth.,
- Alf
More information about the Python-list
mailing list