传送门:http://www.pythontip.com/coding/code_oj_case/8
给你一个整数列表L, 输出L的中位数(若结果为小数,则保留一位小数)。
例如: L=[0,1,2,3,4]
则输出:2
万恶的Python2.7 就是过不了。懒得改代码了。以后本博客只提供python3.5的代码
过不了就过不了,能解决需求就行
L = [10,20,31,40]
lenth = len(L)
if not lenth%2:#如果列表有偶数个元素
num = L[lenth//2-1]+L[lenth//2]
if num%2:#如果中位数为小数
result = num / 2
print('%.1f' % result)
else:
result = num//2
print(result)
else:
result = L[lenth//2]
print(result)