On Mar 30, 4:25 pm, s...@sig.for.address (Victor Eijkhout) wrote:
> 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.

Alf's recommendation is clean and correct.  Just make a list of
tuples.

FWIW, here's a little hack that does the work for you:

>>> values = ['A', 'B', 'C', 'D', 'E']
>>> keys = [50, 20, 40, 10, 30]
>>> keyiter = iter(keys)
>>> sorted(values, key=lambda k: next(keyiter))
['D', 'B', 'E', 'C', 'A']


Raymond
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to