在Python中如何汇总float的列表

时间:2015-12-21 18:49:13

标签: list

我试图将变量l加起来,但是我得到的是浮点数列表而不是一个数字。 这是我的代码:

import re
fname = raw_input("Enter file name: ")
hand = open(fname)
for line in hand:
    line = line.rstrip()
    if not line.startswith("X-DSPAM-Confidence:") : continue
    for lines in line:
        x = re.findall('^X\S*: ([0-9.]+)', line)
        l = map(float, x)
    print sum(l)

1 个答案:

答案 0 :(得分:0)

我认为这应该可以解决问题:

total = 0
for line in hand:
   line = line.rstrip()
   if not line.startswith("X-DSPAM-Confidence:") : continue
   x = re.findall('^X\S*: ([0-9.]+)', line)
   total = total + float(x[0])
print total