How can I find median with an even amount of numbers in a list?

安稳与你 提交于 2020-01-30 09:32:27

问题


This is what I have right now. It just finds the median with an odd amount of numbers.

def median(height):
    height.sort()
    x = len(height)
    x -= 1
    posn = x // 2
    return height[posn]

回答1:


"The median is the numeric value separating the higher half of a sample data set from the lower half. The median of a data set can be found by arranging all the values from lowest to highest value and picking the one in the middle. If there is an odd number of data values then the median will be the value in the middle. If there is an even number of data values the median is the mean of the two data values in the middle." - Source

For the data set 1, 1, 2, 5, 6, 6, 9 the median is 5.

For the data set 1, 1, 2, 6, 6, 9 the median is 4. It is the mean of 2 and 6 or, (2+6)/2 = 4.



来源:https://stackoverflow.com/questions/34299332/how-can-i-find-median-with-an-even-amount-of-numbers-in-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!