python 中两个浮点数的平均函数
mean function of two floats in python
我们想优化 Python 中的一些编码,在这种情况下,我们正在查看我们的 'lazy' 编码。为了优化我们的程序,我们要使用 classes 和函数! (我们是 python 的新手),在这种情况下,我们设置了一个名为 Update:
的 class
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
最后我们想要一个列表显示:{33.31, 32.44, 31.56, 30.69, 29.81}
不幸的是,我们的 mean 函数没有按预期工作,当我们 运行 我们的程序收到以下错误:
/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_list。
对于一般任务,我建议您查看 numpy's linespace.
import numpy as np
first = 33.31
last = 29.81
means = np.linspace(first, last, 5)
print(means)
> [33.31 32.435 31.56 30.685 29.81 ]
您自然可以在 class 方法中使用它。
我们想优化 Python 中的一些编码,在这种情况下,我们正在查看我们的 'lazy' 编码。为了优化我们的程序,我们要使用 classes 和函数! (我们是 python 的新手),在这种情况下,我们设置了一个名为 Update:
的 classclass 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
最后我们想要一个列表显示:{33.31, 32.44, 31.56, 30.69, 29.81}
不幸的是,我们的 mean 函数没有按预期工作,当我们 运行 我们的程序收到以下错误:
/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_list。 对于一般任务,我建议您查看 numpy's linespace.
import numpy as np
first = 33.31
last = 29.81
means = np.linspace(first, last, 5)
print(means)
> [33.31 32.435 31.56 30.685 29.81 ]
您自然可以在 class 方法中使用它。