我正在尝试找到偶数和奇数列表的中位数。这是一个很长的故事,但我必须以这种方式找到中位数(不导入任何东西)。
当我传入 myOddList 作为参数时,中位数是 3(正确)。然而,当我传入 myEvenList 作为参数时,我再次得到 3。如何让 myEvenList 正常工作并输出 3.5?
myOddList = [1, 2, 3, 4, 5]
myEvenList = [1, 2, 3, 4, 5, 6]
def medianCalculator(myEvenList):
myOddList.sort()
myEvenList.sort()
n = len(myOddList)
s = len(myEvenList)
if n % 2 != 0:
answerOdd = myOddList[int(n/2)]
return answerOdd
else:
one = myEvenList [(s-1) // 2]
two = myEvenList [(s+1) // 2]
answerEven = (one + two) / 2
return answerEven
medianForEvenList = medianCalculator(myEvenList)
print(medianForEvenList)
也许问题是我没有正确返回答案?
FFIVE
慕容708150
BIG阳
缥缈止盈
相关分类