gpt4 book ai didi

python - 如何让我的中值计划发挥作用(第 2 部分)

转载 作者:行者123 更新时间:2023-12-01 04:18:46 25 4
gpt4 key购买 nike

我正在尝试制作一个中值程序。这是我的代码。

def median(list=[]):
x = 0
list = sorted(list)
if len(list) % 2 == 0:
b = int((len(list)/2))
print b
c = int(len(list)/2)
x = [list[b-1],list[c]]
print x
x = float((x[0]+x[1])/2)
print str(x)
else:
print list[((len(list)//2))]

当我运行它时,我得到了这个

>>> median([1,2,3,4])
2
[2, 3]
2.0

发生了什么?

请注意:我不需要任何关于如何做得更好的建议,我只是需要帮助。

最佳答案

试试这个,在除法上寻找 2.0 而不是 2

def median(list=[]):
x = 0
list = sorted(list)
if len(list) % 2 == 0:
b = int((len(list)/2.0))
print b
c = int(len(list)/2.0)
x = [list[b-1],list[c]]
print x
x = float((x[0]+x[1])/2.0)
print str(x)
else:
print list[((len(list)//2))]

关于python - 如何让我的中值计划发挥作用(第 2 部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com