title: Map each list value to its corresponding percentile
body: <p>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.</p>
  
  <p>For example, <code>fn([1,2,3,4,17])</code> returns <code>[0.0, 0.25, 0.50, 0.75, 1.00]</code>.</p>
  
  <p>Can anyone please either:</p>
  
  <ol>
  <li>Help me correct my code below? OR</li>
  <li>Offer a better alternative than my code for mapping values in a list to their corresponding percentiles?</li>
  </ol>
  
  <p>My current code:</p>
  
  <pre><code>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] &lt; x[i]:
                      subList_elementCount = subList_elementCount + 1 
              x_percentile[i] = float(subList_elementCount / list_elementCount)
              #x_percentile[i] = float(len(x[x &gt; listFromSetFromList[uCount]]) / list_elementCount)
              if i == 0:
                  continue
              else:
                  if x[i] == x[i-1]:
                      continue
                  else:
                      uCount = uCount + 1
      return x_percentile
  </code></pre>
  
  <p>Currently, if I submit <code>percentile([1,2,3,4,17])</code>, the list <code>[0.0, 0.0, 0.5, 0.0, 1.0]</code> is returned.</p>