Efficient method to calculate the rank vector of a list in Python

后端 未结 11 921
温柔的废话
温柔的废话 2020-12-02 20:06

I\'m looking for an efficient way to calculate the rank vector of a list in Python, similar to R\'s rank function. In a simple list with no ties between the ele

11条回答
  •  伪装坚强ぢ
    2020-12-02 20:55

    [sorted(l).index(x) for x in l]
    

    sorted(l) will give the sorted version index(x) will give the index in the sorted array

    for example :

    l = [-1, 3, 2, 0,0]
    >>> [sorted(l).index(x) for x in l]
    [0, 4, 3, 1, 1]
    

提交回复
热议问题