长度len为啥要减一,还有为啥要pow0.5,开方后不就成了标准差了吗
以下是代码注释:
def dev(numbers, mean):
# 计算标准差的函数
sdev = 0.0 # 初始化标准差变量
# 遍历输入的数字列表
for num in numbers:
# 计算每个数字与平均值的差的平方,并累加到标准差变量中
sdev = sdev + (num - mean) ** 2
# 计算标准差的平方根并返回
return pow(sdev / (len(numbers) - 1), 0.5)
不知道你这个问题是否已经解决, 如果还没有解决的话:>>> t=(1,2,3,4,5)
>>> t
(1, 2, 3, 4, 5)
>>> t(1)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
t(1)
TypeError: 'tuple' object is not callable
>>> t[2]
3
>>> y=t[:]
>>> y
(1, 2, 3, 4, 5)
>>> t[2:3]
(3,)
>>> t[1:3]
(2, 3)
>>> t[:6]
(1, 2, 3, 4, 5)
>>> t[:6]
(1, 2, 3, 4, 5)
>>> type(t)
<class 'tuple'>
>>> t[]
SyntaxError: invalid syntax
>>> t=t[]
SyntaxError: invalid syntax
>>> t=[]
>>> t
[]
>>> t=()
>>> t
()
>>> type(t)
<class 'tuple'>
>>> t=t(4) #不能赋值
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
t=t(4)
TypeError: 'tuple' object is not callable
>>> (6)
6
>>> 4*(6)
24
>>> t=(6,)
>>> 4*t
(6, 6, 6, 6)
>>> t=[1]
>>> 4*t
[1, 1, 1, 1]