I want to write a function median_index(A)
that returns the index of the median given a distribution A ( A is a list with values associated with each index).
I have sorted out the way to get the median value but now I want to have the position (index) where that median is when the list A is sorted.
def median_index(P):
l = sorted(P)
l_len = len(P)
if l_len < 1:
return None
if l_len % 2 == 0 :
return ( l[(l_len-1)/2] + l[(l_len+1)/2] ) / 2.0
else:
return l[(l_len-1)/2]
median_index([0.12,0.04,0.12,0.12,0.2,0.16,0.16,0.08])
That produces 5 as the result. 5 is the last position where 0.12. This is the index of the median 0.12 when I sort the list and calculate the median which is 0.12.
You can use list.index(element)