python中两个浮点数的平均函数
我们希望优化Python中的一些编码,在这种情况下,我们将研究我们的“惰性”编码。为了优化我们的程序,我们希望使用类和函数!(我们是python新手),在这种情况下,我们建立了一个名为Update的类:python中两个浮点数的平均函数,python,mean,Python,Mean,我们希望优化Python中的一些编码,在这种情况下,我们将研究我们的“惰性”编码。为了优化我们的程序,我们希望使用类和函数!(我们是python新手),在这种情况下,我们建立了一个名为Update的类: class Update: def __init__(self, first, last): self.first = first self.last = last def mean(self): print (self.firs
class Update:
def __init__(self, first, last):
self.first = first
self.last = last
def mean(self):
print (self.first, self.last)
return ((self.first + self.last) / 2)
我们想用我们的程序做的是从2个浮点数中生成3倍的新平均值
例如,我们从以下内容开始:
first[-1] = 33.31
last[-1] = 29.81
average = 31.56
然后我们要取上平均值:
(29.81+31.56)/2 = 30.69
(33.31+31.56)/2 = 32.44
较低平均值:
(29.81+31.56)/2 = 30.69
(33.31+31.56)/2 = 32.44
最后我们想要一个列表显示:{33.31,32.44,31.56,30.69,29.81}
不幸的是,我们的平均函数没有按预期工作,当我们运行程序时,我们收到以下错误:
/home/user/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py:3257: RuntimeWarning: Mean of empty slice.
out=out, **kwargs)
/home/user/.local/lib/python3.6/site-packages/numpy/core/_methods.py:161: RuntimeWarning: invalid value encountered in double_scalars
ret = ret.dtype.type(ret / rcount)
好的,这就是我们要做的。请记住,我们被困在两个值的平均值上,然后我们将能够找到上下平均值。注释代码行是程序最初的编程方式,也是我们想要优化的
upd = Update(first[-1], last[-1]) # first = 33.31 and last = 29.81
# f = first[-1]
# l = last[-1]
mean_list = []
# mean_list.append(f)
# mean_list.append(l)
first_mean = []
third_mean = []
# mean = np.mean(mean_list)
# first_mean.append(f)
# third_mean.append(l)
first_mean.append(upd.first)
third_mean.append(upd.last)
# first_mean.append(mean)
# third_mean.append(mean)
mean = upd.mean()
first_mean.append(mean)
third_mean.append(mean)
a = np.mean(first_mean).round(2)
b = np.mean(third_mean).round(2)
mean = np.mean(mean_list).round(2)
您眼前的问题似乎是空变量mean_列表。 对于一般的任务,我建议你仔细研究一下
您可以在类方法中自然地使用它。我注意到您从未在
mean\u列表
中添加任何内容。这是故意的吗?np.means([])
返回是什么意思?