3

A common problem of mine is the following:

As input I have (n is some int >1)

W = numpy.array(...)
L = list(...)

where

len(W) == n
>> true
shape(L)[0] == n
>> true

And I want to sort the list L regarding the values of W and a comparator. My idea was to do the following:

def my_zip_sort(W,L):
    srt = argsort(W)
    return zip(L[srt],W[srt])

This should work like this:

a = ['a', 'b', 'c', 'd']
b = zeros(4)
b[0]=3;b[1]=2;b[2]=[1];b[3]=4
my_zip_sort(a,b)
>> [(c,1)(b,2)(a,3)(d,4)]

But this does not, because

TypeError: only integer arrays with one element can be converted to an index

thus, I need to do another loop:

def my_zip_sort(W,L):
    srt = argsort(W)
    res = list()
    for i in L:
        res.append((L[srt[i]],W[srt[i]]))
    return res

I found a thread about a MaskableList, but this does not work for me (as you can read in the comments), because I would not only need to hold or discard particular values of my list, but also need to re-order them:

a.__class__
>> msk.MaskableList
srt = argsort(b)
a[srt]
>> ['a', 'b', 'd']

Concluding:

I want to find a way to sort a list of objects by constraints in an array. I found a way myself, which is kind of nice except for the list-indexing. Can you help me to write a class that works likewise to MaskableList for this task, which has a good performance?

Community
  • 1
  • 1
Milla Well
  • 3,193
  • 3
  • 35
  • 50

1 Answers1

1

You don't need to extend list do avoid the for-loop. A list-comprehension is sufficient and probably the best you can do here, if you expect a new list of tuples:

def my_zip_sort(W, L):
    srt = argsort(W)
    return [(L[i], W[i]) for i in srt]

Example:

n = 5
W = np.random.randint(10,size=5)
L = [chr(ord('A') + i) for i in W]

L # => ['A', 'C', 'H', 'G', 'C']

srt = np.argsort(W)
result = [(L[i], W[i]) for i in srt]


print result
[('A', 0), ('C', 2), ('C', 2), ('G', 6), ('H', 7)]
tzelleke
  • 15,023
  • 5
  • 33
  • 49
  • That is great! I just built `class MultiIndexList(list): def __getitem__(self,indices): try: return list(self)[indices] except(TypeError): return [self[i] for i in indices]` – Milla Well Feb 07 '13 at 17:23