Hello User9123,
Main Use of the Medain() function
This module provides functions for calculating mathematical statistics of numeric (Real-valued) data
.
Note Unless explicitly noted otherwise, these functions support int, float, decimal.Decimal and fractions.Fraction. Behaviour with other types (whether in the numeric tower or not) is currently unsupported. Mixed types are also undefined and implementation-dependent. If your input data consists of mixed types, you may be able to use map() to ensure a consistent result, e.g. map(float, input_data).
Your Problem
When you use this function for the string so it is not work properly some time so better i suggest you used custom function. I give the solution in my below code.
Explanation Function Median()
statistics.median(data)
Return the median (middle value) of numeric data, using the common “mean of middle two” method. If data is empty, StatisticsError is raised. data can be a sequence or iterator.
The median is a robust measure of central location, and is less affected by the presence of outliers in your data. When the number of data points is odd, the middle data point is returned:
Solution of Problem
If you use this function for the numeric value so try this below code,
def getMedian(numericValues):
theValues = sorted(numericValues)
if len(theValues) % 2 == 1:
return theValues[(len(theValues)+1)/2-1]
else:
lower = theValues[len(theValues)/2-1]
upper = theValues[len(theValues)/2]
return (float(lower + upper)) / 2
print getMedian([0,1,2,3,4,5]) # output = 2.5
If you are use median function for the string so try this below code,
def medianFind(mystring):
return mystring[(len(mystring)-1)/2] #len returns the length of the string
print medianFind("vmr") #prints m
I hope my answer is helpful.
If any query so comment please.