1d list indexing python: enhance MaskableList

偶尔善良 提交于 2019-12-23 19:24:14

问题


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?


回答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)]


来源:https://stackoverflow.com/questions/14664333/1d-list-indexing-python-enhance-maskablelist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!