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

后端 未结 11 953
温柔的废话
温柔的废话 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:51

    This works for the spearman correlation coefficient .

    def get_rank(X, n):
        x_rank = dict((x, i+1) for i, x in enumerate(sorted(set(X))))
        return [x_rank[x] for x in X]
    

提交回复
热议问题