Map each list value to its corresponding percentile

后端 未结 9 563
夕颜
夕颜 2020-11-29 00:11

I\'d like to create a function that takes a (sorted) list as its argument and outputs a list containing each element\'s corresponding percentile.

For example,

9条回答
  •  春和景丽
    2020-11-29 00:45

    this might look oversimplyfied but what about this:

    def percentile(x):
        pc = float(1)/(len(x)-1)
        return ["%.2f"%(n*pc) for n, i in enumerate(x)]
    

    EDIT:

    def percentile(x):
        unique = set(x)
        mapping = {}
        pc = float(1)/(len(unique)-1)
        for n, i in enumerate(unique):
            mapping[i] = "%.2f"%(n*pc)
        return [mapping.get(el) for el in x]
    

提交回复
热议问题