def median(x):
if range(1, len(x))%2!=0:
sorted(x)
listlength = range(1, len(x)+1)
num = listlength / 2
middlenum = x[num]
else:
sorted(x)
listlength = range(1, len(x))
num = listlength / 2
num2 = num + 1
middlenum = x[num2 + num / 2]
return middlenum
This is my code, I need to find a median value of a list (x) but I'm getting this error :
"Oops, try again. median([1]) resulted in an error: unsupported operand type(s) for %: 'list' and 'int' "
I don't really know what to do, please help.
You could also use NumPy's built-in functions, which could potentially be faster.
import numpy as np
def median(x):
return np.median(np.array(x))
NumPy has a whole suite of array-based data analysis functions, such as Mean, Mode, Range, Standard Deviation and more: https://docs.scipy.org/doc/numpy/reference/routines.statistics.html
Hope this helps!