Сопоставьте каждое значение списка с соответствующим процентилем

Я хотел бы создать функцию, которая принимает (отсортированный) список в качестве аргумента и выводит список, содержащий соответствующий процентиль каждого элемента.

Например,fn([1,2,3,4,17]) возвращается[0.0, 0.25, 0.50, 0.75, 1.00].

Может кто-нибудь, пожалуйста, либо:

Help me correct my code below? OR Offer a better alternative than my code for mapping values in a list to their corresponding percentiles?

Мой текущий код:

def median(mylist):
    length = len(mylist)
    if not length % 2:
        return (mylist[length / 2] + mylist[length / 2 - 1]) / 2.0
    return mylist[length / 2]

###############################################################################
# PERCENTILE FUNCTION
###############################################################################

def percentile(x):
    """
    Find the correspoding percentile of each value relative to a list of values.
    where x is the list of values
    Input list should already be sorted!
    """

    # sort the input list
    # list_sorted = x.sort()

    # count the number of elements in the list
    list_elementCount = len(x)

    #obtain set of values from list

    listFromSetFromList = list(set(x))

    # count the number of unique elements in the list
    list_uniqueElementCount = len(set(x))

    # define extreme quantiles
    percentileZero    = min(x)
    percentileHundred = max(x)

    # define median quantile
    mdn = median(x) 

    # create empty list to hold percentiles
    x_percentile = [0.00] * list_elementCount 

    # initialize unique count
    uCount = 0

    for i in range(list_elementCount):
        if x[i] == percentileZero:
            x_percentile[i] = 0.00
        elif x[i] == percentileHundred:
            x_percentile[i] = 1.00
        elif x[i] == mdn:
            x_percentile[i] = 0.50 
        else:
            subList_elementCount = 0
            for j in range(i):
                if x[j] < x[i]:
                    subList_elementCount = subList_elementCount + 1 
            x_percentile[i] = float(subList_elementCount / list_elementCount)
            #x_percentile[i] = float(len(x[x > listFromSetFromList[uCount]]) / list_elementCount)
            if i == 0:
                continue
            else:
                if x[i] == x[i-1]:
                    continue
                else:
                    uCount = uCount + 1
    return x_percentile

В настоящее время, если я отправлюpercentile([1,2,3,4,17]), список[0.0, 0.0, 0.5, 0.0, 1.0] возвращается

Ответы на вопрос(8)

Ваш ответ на вопрос