Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对从文档中提取的数字列表调用sum()_Python - Fatal编程技术网

Python 对从文档中提取的数字列表调用sum()

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(":") +

我需要一些关于求和函数的帮助。我正在尝试查找文档中前缀为“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) 打印(总数/计数)
我不知道我是否正确理解了这一点,但据我所知,您只需要将值的总和存储在变量中,类似于:

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)