Python 对从文档中提取的数字列表调用sum()
我需要一些关于求和函数的帮助。我正在尝试查找文档中前缀为“X-DSPAM-Confidence:”的所有行。在提取它们之后,我想对它们调用sum()并计算平均值。谢谢,堆 for line in (fhand): line = line.rstrip() if not line.startswith("X-DSPAM-Confidence:"): continue else: n = float(line[line.find(":") + 1:]) a = sum(n) count = count + 1 print (n) print (a) print (total / count) 对于线路输入(fhand): line=line.rstrip() 如果不是line.startswith(“X-DSPAM-Confidence:”): 持续 其他: n=浮点(第[line.find(“:”)+1:]行) a=总和(n) 计数=计数+1 打印(n) 印刷品(a) 打印(总数/计数)Python 对从文档中提取的数字列表调用sum(),python,Python,我需要一些关于求和函数的帮助。我正在尝试查找文档中前缀为“X-DSPAM-Confidence:”的所有行。在提取它们之后,我想对它们调用sum()并计算平均值。谢谢,堆 for line in (fhand): line = line.rstrip() if not line.startswith("X-DSPAM-Confidence:"): continue else: n = float(line[line.find(":") +
我不知道我是否正确理解了这一点,但据我所知,您只需要将值的总和存储在变量中,类似于:
total = 0.0
count = 0
for line in (fhand):
line = line.rstrip()
if not line.startswith("X-DSPAM-Confidence:"):
continue
else:
n = float(line[line.find(":") + 1:])
total += n
count = count + 1
print (total / count)
我不知道我是否正确理解了这一点,但据我所知,您只需要将值的总和存储在变量中,类似于:
total = 0.0
count = 0
for line in (fhand):
line = line.rstrip()
if not line.startswith("X-DSPAM-Confidence:"):
continue
else:
n = float(line[line.find(":") + 1:])
total += n
count = count + 1
print (total / count)