Search code examples
pythonlambdamedianpercentile

pure python implementation for calculating percentiles: what is the use of the lambda function here?


I have stumbled upon this pure python implementation for calculating percentiles here and here:

import math
import functools

def percentile(N, percent, key=lambda x:x):
"""
Find the percentile of a list of values.

@parameter N - is a list of values. Note N MUST BE already sorted.
@parameter percent - a float value from 0.0 to 1.0.
@parameter key - optional key function to compute value from each element of N.

@return - the percentile of the values
"""
   if not N:
       return None
   k = (len(N)-1) * percent
   f = math.floor(k)
   c = math.ceil(k)
   if f == c:
       return key(N[int(k)])
   d0 = key(N[int(f)]) * (c-k)
   d1 = key(N[int(c)]) * (k-f)
   return d0+d1

I get the basic principle behind this function and i see that it works correctly:

>>> percentile(range(10),0.25)
2.25

What I don't get is what the lambda function key=lambda x:x is there for. As far as i unterstand it, this lambda function simply returns the value that is passed to it. Basically, the whole function seems to yield the same result if i omit this lambda function altogether:

import math

def percentile2(N, percent):
"""
Find the percentile of a list of values.

@parameter N - is a list of values. Note N MUST BE already sorted.
@parameter percent - a float value from 0.0 to 1.0.
@parameter key - REMOVED

@return - the percentile of the values
"""
   if not N:
       return None
   k = (len(N)-1) * percent
   f = math.floor(k)
   c = math.ceil(k)
   if f == c:
       return N[int(k)]
   d0 = N[int(f)] * (c-k)
   d1 = N[int(c)] * (k-f)
   return d0+d1

If I test this:

>>> percentile2(range(10),0.25)
2.25

So what is the use of that lambda function, here?


Solution

  • The answer is right there in the docstring (the string that starts on the line after the def statement):

    @parameter key - optional key function to compute value from each element of N.
    

    This allows you to use a list of something other than numbers. For example, your lambda could be lambda x:x.getRelevantValue() and your list would be one containing objects that have a getRelevantValue method.